Skip to content

BUG: IntervalIndex.from_arrays with mismatched datetimelike resos #55714

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 27, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,7 @@ Strings
Interval
^^^^^^^^
- Bug in :class:`Interval` ``__repr__`` not displaying UTC offsets for :class:`Timestamp` bounds. Additionally the hour, minute and second components will now be shown. (:issue:`55015`)
- Bug in :meth:`IntervalIndex.from_arrays` when passed ``datetime64`` or ``timedelta64`` arrays with mismatched resolutions constructing an invalid ``IntervalArray`` object (:issue:`55714`)
- Bug in :meth:`IntervalIndex.get_indexer` with datetime or timedelta intervals incorrectly matching on integer targets (:issue:`47772`)
- Bug in :meth:`IntervalIndex.get_indexer` with timezone-aware datetime intervals incorrectly matching on a sequence of timezone-naive targets (:issue:`47772`)
- Bug in setting values on a :class:`Series` with an :class:`IntervalIndex` using a slice incorrectly raising (:issue:`54722`)
Expand Down
5 changes: 5 additions & 0 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,11 @@ def _ensure_simple_new_inputs(
f"'{left.tz}' and '{right.tz}'"
)
raise ValueError(msg)
elif needs_i8_conversion(left.dtype) and left.unit != right.unit:
# e.g. m8[s] vs m8[ms], try to cast to a common dtype GH#55714
left_arr, right_arr = left._data._ensure_matching_resos(right._data)
left = ensure_index(left_arr)
right = ensure_index(right_arr)

# For dt64/td64 we want DatetimeArray/TimedeltaArray instead of ndarray
left = ensure_wrapped_if_datetimelike(left)
Expand Down
23 changes: 23 additions & 0 deletions pandas/tests/indexes/interval/test_constructors.py
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,29 @@ def test_mixed_float_int(self, left_subtype, right_subtype):
tm.assert_index_equal(result.right, expected_right)
assert result.dtype.subtype == expected_subtype

@pytest.mark.parametrize("interval_cls", [IntervalArray, IntervalIndex])
def test_from_arrays_mismatched_datetimelike_resos(self, interval_cls):
# GH#55714
left = date_range("2016-01-01", periods=3, unit="s")
right = date_range("2017-01-01", periods=3, unit="ms")
result = interval_cls.from_arrays(left, right)
expected = interval_cls.from_arrays(left.as_unit("ms"), right)
tm.assert_equal(result, expected)

# td64
left2 = left - left[0]
right2 = right - left[0]
result2 = interval_cls.from_arrays(left2, right2)
expected2 = interval_cls.from_arrays(left2.as_unit("ms"), right2)
tm.assert_equal(result2, expected2)

# dt64tz
left3 = left.tz_localize("UTC")
right3 = right.tz_localize("UTC")
result3 = interval_cls.from_arrays(left3, right3)
expected3 = interval_cls.from_arrays(left3.as_unit("ms"), right3)
tm.assert_equal(result3, expected3)


class TestFromBreaks(ConstructorTests):
"""Tests specific to IntervalIndex.from_breaks"""
Expand Down