Skip to content

Commit 04328a7

Browse files
Backport PR #43428: Regression in __getitem__ raising for slice DatetimeIndex (#43475)
Co-authored-by: Patrick Hoefler <[email protected]>
1 parent 0d8378a commit 04328a7

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

doc/source/whatsnew/v1.3.3.rst

+1
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Fixed regressions
2323
- Fixed regression in :meth:`read_parquet` where the ``fastparquet`` engine would not work properly with fastparquet 0.7.0 (:issue:`43075`)
2424
- Fixed regression in :meth:`DataFrame.loc.__setitem__` raising ``ValueError`` when setting array as cell value (:issue:`43422`)
2525
- Fixed regression in :func:`is_list_like` where objects with ``__iter__`` set to ``None`` would be identified as iterable (:issue:`43373`)
26+
- Fixed regression in :meth:`DataFrame.__getitem__` raising error for slice of :class:`DatetimeIndex` when index is non monotonic (:issue:`43223`)
2627
- Fixed regression in :meth:`.Resampler.aggregate` when used after column selection would raise if ``func`` is a list of aggregation functions (:issue:`42905`)
2728
- Fixed regression in :meth:`DataFrame.corr` where Kendall correlation would produce incorrect results for columns with repeated values (:issue:`43401`)
2829
- Fixed regression in :meth:`Series.fillna` raising ``TypeError`` when filling ``float`` ``Series`` with list-like fill value having a dtype which couldn't cast lostlessly (like ``float32`` filled with ``float64``) (:issue:`43424`)

pandas/core/frame.py

+3
Original file line numberDiff line numberDiff line change
@@ -3433,6 +3433,9 @@ def __getitem__(self, key):
34333433
indexer = lib.maybe_indices_to_slice(
34343434
indexer.astype(np.intp, copy=False), len(self)
34353435
)
3436+
if isinstance(indexer, np.ndarray):
3437+
# GH#43223 If we can not convert, use take
3438+
return self.take(indexer, axis=0)
34363439
# either we have a slice or we have a string that can be converted
34373440
# to a slice for partial-string date indexing
34383441
return self._slice(indexer, axis=0)

pandas/tests/frame/indexing/test_getitem.py

+24
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
CategoricalDtype,
99
CategoricalIndex,
1010
DataFrame,
11+
DatetimeIndex,
1112
Index,
1213
MultiIndex,
1314
Series,
@@ -363,3 +364,26 @@ def test_getitem_slice_float64(self, frame_or_series):
363364

364365
result = obj.loc[start:end]
365366
tm.assert_equal(result, expected)
367+
368+
def test_getitem_datetime_slice(self):
369+
# GH#43223
370+
df = DataFrame(
371+
{"a": 0},
372+
index=DatetimeIndex(
373+
[
374+
"11.01.2011 22:00",
375+
"11.01.2011 23:00",
376+
"12.01.2011 00:00",
377+
"2011-01-13 00:00",
378+
]
379+
),
380+
)
381+
with tm.assert_produces_warning(FutureWarning):
382+
result = df["2011-01-01":"2011-11-01"]
383+
expected = DataFrame(
384+
{"a": 0},
385+
index=DatetimeIndex(
386+
["11.01.2011 22:00", "11.01.2011 23:00", "2011-01-13 00:00"]
387+
),
388+
)
389+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)