Skip to content
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -958,7 +958,7 @@ Interval
^^^^^^^^
- :meth:`Index.is_monotonic_decreasing`, :meth:`Index.is_monotonic_increasing`, and :meth:`Index.is_unique` could incorrectly be ``False`` for an ``Index`` created from a slice of another ``Index``. (:issue:`57911`)
- Bug in :func:`interval_range` where start and end numeric types were always cast to 64 bit (:issue:`57268`)
-
- 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`)

Indexing
^^^^^^^^
Expand Down
13 changes: 13 additions & 0 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,19 @@ def __new__(
dtype=dtype,
)

# Check for mismatched signed/unsigned integer dtypes after casting
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

im confused as to why this affects from_arrays, since that goes through simple_new and not __new__

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure but could it be as in simple_new- IntervalMixin.__new__ is called not IntervalArray.__new__ .

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

seeing as how the added test is failing, my guess is "it doesn't". Maybe put the check at the end of _ensure_simple_new_inputs?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, i'll move at the end of _ensure_simple_new_inputs.

left_dtype = left.dtype
right_dtype = right.dtype
if (
left_dtype.kind in "iu"
and right_dtype.kind in "iu"
and left_dtype.kind != right_dtype.kind
):
raise TypeError(
f"Left and right arrays must have matching signedness. "
f"Got {left_dtype} and {right_dtype}."
)

if verify_integrity:
cls._validate(left, right, dtype=dtype)

Expand Down
2 changes: 1 addition & 1 deletion pandas/io/excel/_xlsxwriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def __init__( # pyright: ignore[reportInconsistentConstructor]
)

try:
self._book = Workbook(self._handles.handle, **engine_kwargs) # type: ignore[arg-type]
self._book = Workbook(self._handles.handle, **engine_kwargs)
except TypeError:
self._handles.handle.close()
raise
Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/indexes/interval/test_interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,14 @@ def test_is_all_dates(self):
assert not year_2017_index._is_all_dates


def test_from_arrays_mismatched_signedness_raises():
# GH 55715
left = np.array([0, 1, 2], dtype="int64")
right = np.array([1, 2, 3], dtype="uint64")
with pytest.raises(TypeError, match="matching signedness"):
IntervalIndex.from_arrays(left, right)


def test_dir():
# GH#27571 dir(interval_index) should not raise
index = IntervalIndex.from_arrays([0, 1], [1, 2])
Expand Down
Loading