Skip to content

Commit

Permalink
BUG: fixes indexing with monotonic decreasing DTI (#19362)
Browse files Browse the repository at this point in the history
  • Loading branch information
mapehe committed Apr 14, 2018
1 parent d104ecd commit db669b5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 1 deletion.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1073,6 +1073,7 @@ Indexing
- Bug in :meth:`Index.difference` when taking difference of an ``Index`` with itself (:issue:`20040`)
- 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`).
- Bug in :class:`IntervalIndex` where some indexing operations were not supported for overlapping or non-monotonic ``uint64`` data (:issue:`20636`)
- Bug in indexing with monotonic decreasing datetimelike (:issue:`19362`)

MultiIndex
^^^^^^^^^^
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,8 @@ def _format_with_header(self, header, **kwargs):
def __contains__(self, key):
try:
res = self.get_loc(key)
return is_scalar(res) or type(res) == slice or np.any(res)
return is_scalar(res) or isinstance(res, slice) or \
(is_list_like(res) and len(res))
except (KeyError, TypeError, ValueError):
return False

Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/indexes/datetimes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,3 +368,14 @@ def test_factorize_dst(self):
def test_unique(self, arr, expected):
result = arr.unique()
tm.assert_index_equal(result, expected)

def test_monotone_DTI_indexing_bug(self):
# GH 19362

df = pd.DataFrame({'A': [1, 2, 3]},
index=pd.date_range('20170101',
periods=3)[::-1])
expected = pd.DataFrame({'A': 1},
index=pd.date_range('20170103',
periods=1))
tm.assert_frame_equal(df.loc['2017-01-03'], expected)

0 comments on commit db669b5

Please sign in to comment.