Skip to content

Commit 736ae02

Browse files
committed
BUG: Raise TypeError for mismatched signed/unsigned dtypes in IntervalIndex.from_arrays
1 parent b2a6d74 commit 736ae02

File tree

2 files changed

+22
-0
lines changed

2 files changed

+22
-0
lines changed

pandas/core/indexes/interval.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,20 @@ def from_arrays(
310310
copy: bool = False,
311311
dtype: Dtype | None = None,
312312
) -> IntervalIndex:
313+
# Check for mismatched signed/unsigned integer dtypes
314+
left_dtype = getattr(left, "dtype", None)
315+
right_dtype = getattr(right, "dtype", None)
316+
if (
317+
left_dtype is not None
318+
and right_dtype is not None
319+
and left_dtype.kind in "iu"
320+
and right_dtype.kind in "iu"
321+
and left_dtype.kind != right_dtype.kind
322+
):
323+
raise TypeError(
324+
f"Left and right arrays must have matching signedness. "
325+
f"Got {left_dtype} and {right_dtype}."
326+
)
313327
with rewrite_exception("IntervalArray", cls.__name__):
314328
array = IntervalArray.from_arrays(
315329
left, right, closed, copy=copy, dtype=dtype

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)