Skip to content

Commit 425ce1b

Browse files
authored
REGR: fix bug in DatetimeIndex slicing with irregular or unsorted indices (#37023)
1 parent 348dc2d commit 425ce1b

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
@@ -22,6 +22,7 @@ Fixed regressions
2222
- Fixed regression in :class:`RollingGroupby` causing a segmentation fault with Index of dtype object (:issue:`36727`)
2323
- Fixed regression in :meth:`DataFrame.resample(...).apply(...)` raised ``AttributeError`` when input was a :class:`DataFrame` and only a :class:`Series` was evaluated (:issue:`36951`)
2424
- Fixed regression in :class:`PeriodDtype` comparing both equal and unequal to its string representation (:issue:`37265`)
25+
- 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`)
2526
- Fixed regression in certain offsets (:meth:`pd.offsets.Day() <pandas.tseries.offsets.Day>` and below) no longer being hashable (:issue:`37267`)
2627
- Fixed regression in :class:`StataReader` which required ``chunksize`` to be manually set when using an iterator to read a dataset (:issue:`37280`)
2728

pandas/core/frame.py

+2
Original file line numberDiff line numberDiff line change
@@ -2920,6 +2920,8 @@ def __getitem__(self, key):
29202920
# Do we have a slicer (on rows)?
29212921
indexer = convert_to_index_sliceable(self, key)
29222922
if indexer is not None:
2923+
if isinstance(indexer, np.ndarray):
2924+
indexer = lib.maybe_indices_to_slice(indexer, len(self))
29232925
# either we have a slice or we have a string that can be converted
29242926
# to a slice for partial-string date indexing
29252927
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)