Skip to content

Commit db669b5

Browse files
committed
BUG: fixes indexing with monotonic decreasing DTI (#19362)
1 parent d104ecd commit db669b5

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1073,6 +1073,7 @@ Indexing
10731073
- Bug in :meth:`Index.difference` when taking difference of an ``Index`` with itself (:issue:`20040`)
10741074
- Bug in :meth:`DataFrame.first_valid_index` and :meth:`DataFrame.last_valid_index` in presence of entire rows of NaNs in the middle of values (:issue:`20499`).
10751075
- Bug in :class:`IntervalIndex` where some indexing operations were not supported for overlapping or non-monotonic ``uint64`` data (:issue:`20636`)
1076+
- Bug in indexing with monotonic decreasing datetimelike (:issue:`19362`)
10761077

10771078
MultiIndex
10781079
^^^^^^^^^^

pandas/core/indexes/datetimelike.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,8 @@ def _format_with_header(self, header, **kwargs):
342342
def __contains__(self, key):
343343
try:
344344
res = self.get_loc(key)
345-
return is_scalar(res) or type(res) == slice or np.any(res)
345+
return is_scalar(res) or isinstance(res, slice) or \
346+
(is_list_like(res) and len(res))
346347
except (KeyError, TypeError, ValueError):
347348
return False
348349

pandas/tests/indexes/datetimes/test_datetime.py

+11
Original file line numberDiff line numberDiff line change
@@ -368,3 +368,14 @@ def test_factorize_dst(self):
368368
def test_unique(self, arr, expected):
369369
result = arr.unique()
370370
tm.assert_index_equal(result, expected)
371+
372+
def test_monotone_DTI_indexing_bug(self):
373+
# GH 19362
374+
375+
df = pd.DataFrame({'A': [1, 2, 3]},
376+
index=pd.date_range('20170101',
377+
periods=3)[::-1])
378+
expected = pd.DataFrame({'A': 1},
379+
index=pd.date_range('20170103',
380+
periods=1))
381+
tm.assert_frame_equal(df.loc['2017-01-03'], expected)

0 commit comments

Comments
 (0)