Skip to content

Commit 3a2e9e6

Browse files
mapehejreback
authored andcommitted
BUG: fixes indexing with monotonic decreasing DTI (pandas-dev#19362) (pandas-dev#20677)
1 parent be057a1 commit 3a2e9e6

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

doc/source/whatsnew/v0.23.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1113,6 +1113,7 @@ Indexing
11131113
- 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`).
11141114
- Bug in :class:`IntervalIndex` where some indexing operations were not supported for overlapping or non-monotonic ``uint64`` data (:issue:`20636`)
11151115
- Bug in ``Series.is_unique`` where extraneous output in stderr is shown if Series contains objects with ``__ne__`` defined (:issue:`20661`)
1116+
- Bug in partial string indexing on a ``Series/DataFrame`` with a monotonic decreasing ``DatetimeIndex`` (:issue:`19362`)
11161117

11171118
MultiIndex
11181119
^^^^^^^^^^

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_partial_slicing.py

+21
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,27 @@ def test_slice_duplicate_monotonic(self):
9191
expected = Timestamp('2017-01-01')
9292
assert result == expected
9393

94+
def test_monotone_DTI_indexing_bug(self):
95+
# GH 19362
96+
# Testing accessing the first element in a montononic descending
97+
# partial string indexing.
98+
99+
df = pd.DataFrame(list(range(5)))
100+
date_list = ['2018-01-02', '2017-02-10', '2016-03-10',
101+
'2015-03-15', '2014-03-16']
102+
date_index = pd.to_datetime(date_list)
103+
df['date'] = date_index
104+
expected = pd.DataFrame({0: list(range(5)), 'date': date_index})
105+
tm.assert_frame_equal(df, expected)
106+
107+
df = pd.DataFrame({'A': [1, 2, 3]},
108+
index=pd.date_range('20170101',
109+
periods=3)[::-1])
110+
expected = pd.DataFrame({'A': 1},
111+
index=pd.date_range('20170103',
112+
periods=1))
113+
tm.assert_frame_equal(df.loc['2017-01-03'], expected)
114+
94115
def test_slice_year(self):
95116
dti = DatetimeIndex(freq='B', start=datetime(2005, 1, 1), periods=500)
96117

0 commit comments

Comments
 (0)