Skip to content

Commit 6e2eb0e

Browse files
committed
BUG: Fixed partial slicing on monotonic index with dupes
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
1 parent f541b34 commit 6e2eb0e

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

doc/source/whatsnew/v0.20.2.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ Indexing
6363
^^^^^^^^
6464

6565
- Bug in ``DataFrame.reset_index(level=)`` with single level index (:issue:`16263`)
66-
66+
- Bug in partial string indexing with a monotonic, but not strictly-monotonic, index incorrectly reversing the slice bounds (:issue:`16515`)
6767

6868
I/O
6969
^^^

pandas/core/indexes/datetimes.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1472,7 +1472,7 @@ def _maybe_cast_slice_bound(self, label, side, kind):
14721472
# the bounds need swapped if index is reverse sorted and has a
14731473
# length > 1 (is_monotonic_decreasing gives True for empty
14741474
# and length 1 index)
1475-
if self.is_monotonic_decreasing and len(self) > 1:
1475+
if self.is_strictly_monotonic_decreasing and len(self) > 1:
14761476
return upper if side == 'left' else lower
14771477
return lower if side == 'left' else upper
14781478
else:

pandas/tests/indexes/datetimes/test_datetime.py

+7
Original file line numberDiff line numberDiff line change
@@ -771,3 +771,10 @@ def test_slice_bounds_empty(self):
771771
left = empty_idx._maybe_cast_slice_bound('2015-01-02', 'left', 'loc')
772772
exp = Timestamp('2015-01-02 00:00:00')
773773
assert left == exp
774+
775+
def test_slice_duplicate_monotonic(self):
776+
# https://github.com/pandas-dev/pandas/issues/16515
777+
idx = pd.DatetimeIndex(['2017', '2017'])
778+
result = idx._maybe_cast_slice_bound('2017-01-01', 'left', 'loc')
779+
expected = Timestamp('2017-01-01')
780+
assert result == expected

0 commit comments

Comments
 (0)