Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

API: ensure IntervalIndex.left/right are 64bit if numeric #50130

Merged
merged 2 commits into from
Dec 11, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@
is_number,
is_object_dtype,
is_scalar,
is_signed_integer_dtype,
is_unsigned_integer_dtype,
)
from pandas.core.dtypes.dtypes import IntervalDtype
from pandas.core.dtypes.missing import is_valid_na_for_dtype
Expand Down Expand Up @@ -521,6 +523,7 @@ def _maybe_convert_i8(self, key):
original = key
if is_list_like(key):
key = ensure_index(key)
key = self._maybe_convert_numeric_to_64bit(key)

if not self._needs_i8_conversion(key):
return original
Expand Down Expand Up @@ -566,6 +569,20 @@ def _maybe_convert_i8(self, key):

return key_i8

def _maybe_convert_numeric_to_64bit(self, idx: Index) -> Index:
# IntervalTree only supports 64 bit numpy array
dtype = idx.dtype
if np.issubclass_(idx.dtype.type, np.number):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you use dtype.type here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes:)

return idx
elif is_signed_integer_dtype(dtype) and dtype != np.int64:
return idx.astype(np.int64)
elif is_unsigned_integer_dtype(dtype) and dtype != np.uint64:
return idx.astype(np.uint64)
elif is_float_dtype(dtype) and dtype != np.float64:
return idx.astype(np.float64)
else:
return idx

def _searchsorted_monotonic(self, label, side: Literal["left", "right"] = "left"):
if not self.is_non_overlapping_monotonic:
raise KeyError(
Expand Down