Skip to content

Commit a009f43

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 a009f43

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pandas/core/indexes/interval.py

+18
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
is_datetime64tz_dtype,
5151
is_datetime_or_timedelta_dtype,
5252
is_dtype_equal,
53+
is_extension_array_dtype,
5354
is_float,
5455
is_float_dtype,
5556
is_integer,
@@ -59,6 +60,8 @@
5960
is_number,
6061
is_object_dtype,
6162
is_scalar,
63+
is_signed_integer_dtype,
64+
is_unsigned_integer_dtype,
6265
)
6366
from pandas.core.dtypes.dtypes import IntervalDtype
6467
from pandas.core.dtypes.missing import is_valid_na_for_dtype
@@ -521,6 +524,7 @@ def _maybe_convert_i8(self, key):
521524
original = key
522525
if is_list_like(key):
523526
key = ensure_index(key)
527+
key = self._maybe_convert_numeric_to_64bit(key)
524528

525529
if not self._needs_i8_conversion(key):
526530
return original
@@ -566,6 +570,20 @@ def _maybe_convert_i8(self, key):
566570

567571
return key_i8
568572

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

0 commit comments

Comments
 (0)