Skip to content

Commit 3c84724

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

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

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)