Skip to content

Commit a951998

Browse files
phofljreback
andauthored
Regression in __getitem__ raising for slice DatetimeIndex (#43428)
* Regression in __getitem__ raising for slice DatetimeIndex * Change to try casting Co-authored-by: Jeff Reback <[email protected]>
1 parent f9762cc commit a951998

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
@@ -24,6 +24,7 @@ Fixed regressions
2424
- Fixed regression in :meth:`read_parquet` where the ``fastparquet`` engine would not work properly with fastparquet 0.7.0 (:issue:`43075`)
2525
- Fixed regression in :meth:`DataFrame.loc.__setitem__` raising ``ValueError`` when setting array as cell value (:issue:`43422`)
2626
- Fixed regression in :func:`is_list_like` where objects with ``__iter__`` set to ``None`` would be identified as iterable (:issue:`43373`)
27+
- Fixed regression in :meth:`DataFrame.__getitem__` raising error for slice of :class:`DatetimeIndex` when index is non monotonic (:issue:`43223`)
2728
- Fixed regression in :meth:`.Resampler.aggregate` when used after column selection would raise if ``func`` is a list of aggregation functions (:issue:`42905`)
2829
- Fixed regression in :meth:`DataFrame.corr` where Kendall correlation would produce incorrect results for columns with repeated values (:issue:`43401`)
2930
- 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
@@ -3473,6 +3473,9 @@ def __getitem__(self, key):
34733473
indexer = lib.maybe_indices_to_slice(
34743474
indexer.astype(np.intp, copy=False), len(self)
34753475
)
3476+
if isinstance(indexer, np.ndarray):
3477+
# GH#43223 If we can not convert, use take
3478+
return self.take(indexer, axis=0)
34763479
# either we have a slice or we have a string that can be converted
34773480
# to a slice for partial-string date indexing
34783481
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,
@@ -364,3 +365,26 @@ def test_getitem_slice_float64(self, frame_or_series):
364365

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

0 commit comments

Comments
 (0)