Skip to content

Commit df22136

Browse files
Backport PR #37023: REGR: fix bug in DatetimeIndex slicing with irregular or unsorted indices (#37418)
Co-authored-by: Tobias Pitters <[email protected]>
1 parent be91f39 commit df22136

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

doc/source/whatsnew/v1.1.4.rst

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Fixed regressions
2121
- Fixed regression in :meth:`Series.astype` converting ``None`` to ``"nan"`` when casting to string (:issue:`36904`)
2222
- Fixed regression in :class:`RollingGroupby` causing a segmentation fault with Index of dtype object (:issue:`36727`)
2323
- Fixed regression in :class:`PeriodDtype` comparing both equal and unequal to its string representation (:issue:`37265`)
24+
- Fixed regression where slicing :class:`DatetimeIndex` raised :exc:`AssertionError` on irregular time series with ``pd.NaT`` or on unsorted indices (:issue:`36953` and :issue:`35509`)
2425
- Fixed regression in certain offsets (:meth:`pd.offsets.Day() <pandas.tseries.offsets.Day>` and below) no longer being hashable (:issue:`37267`)
2526
- Fixed regression in :class:`StataReader` which required ``chunksize`` to be manually set when using an iterator to read a dataset (:issue:`37280`)
2627

pandas/core/frame.py

+2
Original file line numberDiff line numberDiff line change
@@ -2880,6 +2880,8 @@ def __getitem__(self, key):
28802880
# Do we have a slicer (on rows)?
28812881
indexer = convert_to_index_sliceable(self, key)
28822882
if indexer is not None:
2883+
if isinstance(indexer, np.ndarray):
2884+
indexer = lib.maybe_indices_to_slice(indexer, len(self))
28832885
# either we have a slice or we have a string that can be converted
28842886
# to a slice for partial-string date indexing
28852887
return self._slice(indexer, axis=0)

pandas/tests/indexing/test_partial.py

+21
Original file line numberDiff line numberDiff line change
@@ -681,3 +681,24 @@ def test_index_name_empty(self):
681681
{"series": [1.23] * 4}, index=pd.RangeIndex(4, name="series_index")
682682
)
683683
tm.assert_frame_equal(df, expected)
684+
685+
def test_slice_irregular_datetime_index_with_nan(self):
686+
# GH36953
687+
index = pd.to_datetime(["2012-01-01", "2012-01-02", "2012-01-03", None])
688+
df = DataFrame(range(len(index)), index=index)
689+
expected = DataFrame(range(len(index[:3])), index=index[:3])
690+
result = df["2012-01-01":"2012-01-04"]
691+
tm.assert_frame_equal(result, expected)
692+
693+
def test_slice_datetime_index(self):
694+
# GH35509
695+
df = DataFrame(
696+
{"col1": ["a", "b", "c"], "col2": [1, 2, 3]},
697+
index=pd.to_datetime(["2020-08-01", "2020-07-02", "2020-08-05"]),
698+
)
699+
expected = DataFrame(
700+
{"col1": ["a", "c"], "col2": [1, 3]},
701+
index=pd.to_datetime(["2020-08-01", "2020-08-05"]),
702+
)
703+
result = df.loc["2020-08"]
704+
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)