Skip to content

Commit

Permalink
API: ensure IntervalIndex.left/right are 64bit if numeric (#50130)
Browse files Browse the repository at this point in the history
  • Loading branch information
topper-123 committed Dec 11, 2022
1 parent de4525b commit 68537e3
Showing 1 changed file with 17 additions and 0 deletions.
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_(dtype.type, np.number):
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

0 comments on commit 68537e3

Please sign in to comment.