Skip to content

Commit 34af798

Browse files
REGR: fix pd.cut with datetime IntervalIndex as bins (#47771)
1 parent 56eba46 commit 34af798

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

doc/source/whatsnew/v1.4.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ Fixed regressions
1616
~~~~~~~~~~~~~~~~~
1717
- Fixed regression in taking NULL :class:`objects` from a :class:`DataFrame` causing a segmentation violation. These NULL values are created by :meth:`numpy.empty_like` (:issue:`46848`)
1818
- Fixed regression in :func:`concat` materializing :class:`Index` during sorting even if :class:`Index` was already sorted (:issue:`47501`)
19+
- Fixed regression in :func:`cut` using a ``datetime64`` IntervalIndex as bins (:issue:`46218`)
1920
- Fixed regression in :meth:`DataFrame.loc` not updating the cache correctly after values were set (:issue:`47867`)
2021
- Fixed regression in :meth:`DataFrame.loc` not aligning index in some cases when setting a :class:`DataFrame` (:issue:`47578`)
2122
- Fixed regression in :meth:`DataFrame.loc` setting a length-1 array like value to a single value in the DataFrame (:issue:`46268`)

pandas/core/indexes/base.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -3987,8 +3987,14 @@ def _should_partial_index(self, target: Index) -> bool:
39873987
Should we attempt partial-matching indexing?
39883988
"""
39893989
if is_interval_dtype(self.dtype):
3990+
if is_interval_dtype(target.dtype):
3991+
return False
3992+
# See https://github.com/pandas-dev/pandas/issues/47772 the commented
3993+
# out code can be restored (instead of hardcoding `return True`)
3994+
# once that issue if fixed
39903995
# "Index" has no attribute "left"
3991-
return self.left._should_compare(target) # type: ignore[attr-defined]
3996+
# return self.left._should_compare(target) # type: ignore[attr-defined]
3997+
return True
39923998
return False
39933999

39944000
@final

pandas/tests/indexes/interval/test_indexing.py

+15
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from pandas import (
99
NA,
1010
CategoricalIndex,
11+
DatetimeIndex,
1112
Index,
1213
Interval,
1314
IntervalIndex,
@@ -302,6 +303,20 @@ def test_get_indexer_categorical_with_nans(self):
302303
expected = np.array([0, 1, 2, 3, 4, 0, 1, 2, 3, 4], dtype=np.intp)
303304
tm.assert_numpy_array_equal(result, expected)
304305

306+
def test_get_indexer_datetime(self):
307+
ii = IntervalIndex.from_breaks(date_range("2018-01-01", periods=4))
308+
result = ii.get_indexer(DatetimeIndex(["2018-01-02"]))
309+
expected = np.array([0], dtype=np.intp)
310+
tm.assert_numpy_array_equal(result, expected)
311+
312+
result = ii.get_indexer(DatetimeIndex(["2018-01-02"]).astype(str))
313+
tm.assert_numpy_array_equal(result, expected)
314+
315+
# TODO this should probably be deprecated?
316+
# https://github.com/pandas-dev/pandas/issues/47772
317+
result = ii.get_indexer(DatetimeIndex(["2018-01-02"]).asi8)
318+
tm.assert_numpy_array_equal(result, expected)
319+
305320
@pytest.mark.parametrize(
306321
"tuples, closed",
307322
[

pandas/tests/reshape/test_cut.py

+10
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
Timestamp,
1515
cut,
1616
date_range,
17+
interval_range,
1718
isna,
1819
qcut,
1920
timedelta_range,
@@ -734,3 +735,12 @@ def test_cut_with_timestamp_tuple_labels():
734735

735736
expected = Categorical.from_codes([0, 1, 2], labels, ordered=True)
736737
tm.assert_categorical_equal(result, expected)
738+
739+
740+
def test_cut_bins_datetime_intervalindex():
741+
# https://github.com/pandas-dev/pandas/issues/46218
742+
bins = interval_range(Timestamp("2022-02-25"), Timestamp("2022-02-27"), freq="1D")
743+
# passing Series instead of list is important to trigger bug
744+
result = cut(Series([Timestamp("2022-02-26")]), bins=bins)
745+
expected = Categorical.from_codes([0], bins, ordered=True)
746+
tm.assert_categorical_equal(result.array, expected)

0 commit comments

Comments
 (0)