Skip to content

Commit

Permalink
BUG: Fixed partial slicing on monotonic index with dupes
Browse files Browse the repository at this point in the history
Fixed an edge case in partial string indexing where we would incorrectly flip
the endpoint on a slice, since we checked for monotonicity when we needed strict
monotonicity.

Closes #16515
xref dask/dask#2389
  • Loading branch information
TomAugspurger committed May 31, 2017
1 parent f541b34 commit 6e2eb0e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 2 deletions.
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.20.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Indexing
^^^^^^^^

- Bug in ``DataFrame.reset_index(level=)`` with single level index (:issue:`16263`)

- Bug in partial string indexing with a monotonic, but not strictly-monotonic, index incorrectly reversing the slice bounds (:issue:`16515`)

I/O
^^^
Expand Down
2 changes: 1 addition & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1472,7 +1472,7 @@ def _maybe_cast_slice_bound(self, label, side, kind):
# the bounds need swapped if index is reverse sorted and has a
# length > 1 (is_monotonic_decreasing gives True for empty
# and length 1 index)
if self.is_monotonic_decreasing and len(self) > 1:
if self.is_strictly_monotonic_decreasing and len(self) > 1:
return upper if side == 'left' else lower
return lower if side == 'left' else upper
else:
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/datetimes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,3 +771,10 @@ def test_slice_bounds_empty(self):
left = empty_idx._maybe_cast_slice_bound('2015-01-02', 'left', 'loc')
exp = Timestamp('2015-01-02 00:00:00')
assert left == exp

def test_slice_duplicate_monotonic(self):
# https://github.com/pandas-dev/pandas/issues/16515
idx = pd.DatetimeIndex(['2017', '2017'])
result = idx._maybe_cast_slice_bound('2017-01-01', 'left', 'loc')
expected = Timestamp('2017-01-01')
assert result == expected

0 comments on commit 6e2eb0e

Please sign in to comment.