Skip to content

Commit 7e1fbdf

Browse files
Terji PetersenTerji Petersen
Terji Petersen
authored and
Terji Petersen
committed
API: ensure IntervalIndex.left/right are 64bit if numeric
1 parent 3ece807 commit 7e1fbdf

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

pandas/core/indexes/interval.py

+17
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@
5959
is_number,
6060
is_object_dtype,
6161
is_scalar,
62+
is_signed_integer_dtype,
63+
is_unsigned_integer_dtype,
6264
)
6365
from pandas.core.dtypes.dtypes import IntervalDtype
6466
from pandas.core.dtypes.missing import is_valid_na_for_dtype
@@ -521,6 +523,7 @@ def _maybe_convert_i8(self, key):
521523
original = key
522524
if is_list_like(key):
523525
key = ensure_index(key)
526+
key = self._maybe_convert_numeric_to_64bit(key)
524527

525528
if not self._needs_i8_conversion(key):
526529
return original
@@ -566,6 +569,20 @@ def _maybe_convert_i8(self, key):
566569

567570
return key_i8
568571

572+
def _maybe_convert_numeric_to_64bit(self, idx: Index) -> Index:
573+
# IntervalTree only supports 64 bit numpy array
574+
dtype = idx.dtype
575+
if np.issubclass_(idx.dtype.type, np.number):
576+
return idx
577+
elif is_signed_integer_dtype(dtype) and dtype != np.int64:
578+
return idx.astype(np.int64)
579+
elif is_unsigned_integer_dtype(dtype) and dtype != np.uint64:
580+
return idx.astype(np.uint64)
581+
elif is_float_dtype(dtype) and dtype != np.float64:
582+
return idx.astype(np.float64)
583+
else:
584+
return idx
585+
569586
def _searchsorted_monotonic(self, label, side: Literal["left", "right"] = "left"):
570587
if not self.is_non_overlapping_monotonic:
571588
raise KeyError(

0 commit comments

Comments
 (0)