Skip to content

Commit 36f5e25

Browse files
authored
BUG: Raise TypeError for mismatched signed/unsigned dtypes in IntervalIndex.from_arrays (pandas-dev#62376)
1 parent 8e0ae73 commit 36f5e25

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1049,6 +1049,7 @@ Interval
10491049
- Bug in :class:`Index`, :class:`Series`, :class:`DataFrame` constructors when given a sequence of :class:`Interval` subclass objects casting them to :class:`Interval` (:issue:`46945`)
10501050
- Bug in :func:`interval_range` where start and end numeric types were always cast to 64 bit (:issue:`57268`)
10511051
- Bug in :meth:`IntervalIndex.get_indexer` and :meth:`IntervalIndex.drop` when one of the sides of the index is non-unique (:issue:`52245`)
1052+
- Construction of :class:`IntervalArray` and :class:`IntervalIndex` from arrays with mismatched signed/unsigned integer dtypes (e.g., ``int64`` and ``uint64``) now raises a :exc:`TypeError` instead of proceeding silently. (:issue:`55715`)
10521053

10531054
Indexing
10541055
^^^^^^^^

pandas/core/arrays/interval.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,18 @@ def _ensure_simple_new_inputs(
420420

421421
dtype = IntervalDtype(left.dtype, closed=closed)
422422

423+
# Check for mismatched signed/unsigned integer dtypes after casting
424+
left_dtype = left.dtype
425+
right_dtype = right.dtype
426+
if (
427+
left_dtype.kind in "iu"
428+
and right_dtype.kind in "iu"
429+
and left_dtype.kind != right_dtype.kind
430+
):
431+
raise TypeError(
432+
f"Left and right arrays must have matching signedness. "
433+
f"Got {left_dtype} and {right_dtype}."
434+
)
423435
return left, right, dtype
424436

425437
@classmethod

pandas/tests/indexes/interval/test_interval.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -882,6 +882,14 @@ def test_is_all_dates(self):
882882
assert not year_2017_index._is_all_dates
883883

884884

885+
def test_from_arrays_mismatched_signedness_raises():
886+
# GH 55715
887+
left = np.array([0, 1, 2], dtype="int64")
888+
right = np.array([1, 2, 3], dtype="uint64")
889+
with pytest.raises(TypeError, match="matching signedness"):
890+
IntervalIndex.from_arrays(left, right)
891+
892+
885893
def test_dir():
886894
# GH#27571 dir(interval_index) should not raise
887895
index = IntervalIndex.from_arrays([0, 1], [1, 2])

0 commit comments

Comments
 (0)