Skip to content

BUG: IntervalIndex.factorize with non-nano #56099

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
Nov 21, 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 @@ -404,6 +404,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.factorize` and :meth:`Series.factorize` with :class:`IntervalDtype` with datetime64 or timedelta64 intervals not preserving non-nanosecond units (:issue:`56099`)
- 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`)
Expand Down
7 changes: 1 addition & 6 deletions pandas/core/arrays/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,12 +391,7 @@ def _from_sequence(

@classmethod
def _from_factorized(cls, values: np.ndarray, original: IntervalArray) -> Self:
if len(values) == 0:
# An empty array returns object-dtype here. We can't create
# a new IA from an (empty) object-dtype array, so turn it into the
# correct dtype.
values = values.astype(original.dtype.subtype)
return cls(values, closed=original.closed)
return cls._from_sequence(values, dtype=original.dtype)

_interval_shared_docs["from_breaks"] = textwrap.dedent(
"""
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/test_algos.py
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,25 @@ def test_factorize_mixed_values(self, data, expected_codes, expected_uniques):
tm.assert_numpy_array_equal(codes, expected_codes)
tm.assert_index_equal(uniques, expected_uniques)

def test_factorize_interval_non_nano(self, unit):
# GH#56099
left = DatetimeIndex(["2016-01-01", np.nan, "2015-10-11"]).as_unit(unit)
right = DatetimeIndex(["2016-01-02", np.nan, "2015-10-15"]).as_unit(unit)
idx = IntervalIndex.from_arrays(left, right)
codes, cats = idx.factorize()
assert cats.dtype == f"interval[datetime64[{unit}], right]"

ts = Timestamp(0).as_unit(unit)
idx2 = IntervalIndex.from_arrays(left - ts, right - ts)
codes2, cats2 = idx2.factorize()
assert cats2.dtype == f"interval[timedelta64[{unit}], right]"

idx3 = IntervalIndex.from_arrays(
left.tz_localize("US/Pacific"), right.tz_localize("US/Pacific")
)
codes3, cats3 = idx3.factorize()
assert cats3.dtype == f"interval[datetime64[{unit}, US/Pacific], right]"


class TestUnique:
def test_ints(self):
Expand Down