Skip to content

REF: date arg not reachable in DTI._maybe_cast_slice_bound #42855

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -722,7 +722,7 @@ def _maybe_cast_slice_bound(self, label, side: str, kind=lib.no_default):
if self._is_strictly_monotonic_decreasing and len(self) > 1:
return upper if side == "left" else lower
return lower if side == "left" else upper
elif isinstance(label, (self._data._recognized_scalars, date)):
elif isinstance(label, self._data._recognized_scalars):
self._deprecate_mismatched_indexing(label)
else:
raise self._invalid_indexer("slice", label)
Expand Down Expand Up @@ -800,6 +800,13 @@ def check_str_or_none(point):
else:
return indexer

@doc(Index.get_slice_bound)
def get_slice_bound(self, label, side: str, kind=None) -> int:
# GH#42855 handle date here instead of _maybe_cast_slice_bound
if isinstance(label, date) and not isinstance(label, datetime):
label = Timestamp(label).to_pydatetime()
return super().get_slice_bound(label, side=side, kind=kind)

# --------------------------------------------------------------------

@property
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -746,7 +746,7 @@ def test_get_slice_bounds_datetime_within(
result = index.get_slice_bound(key, kind=kind, side=side)
assert result == expected

@pytest.mark.parametrize("box", [date, datetime, Timestamp])
@pytest.mark.parametrize("box", [datetime, Timestamp])
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
@pytest.mark.parametrize("side", ["left", "right"])
@pytest.mark.parametrize("year, expected", [(1999, 0), (2020, 30)])
Expand All @@ -764,7 +764,7 @@ def test_get_slice_bounds_datetime_outside(
result = index.get_slice_bound(key, kind=kind, side=side)
assert result == expected

@pytest.mark.parametrize("box", [date, datetime, Timestamp])
@pytest.mark.parametrize("box", [datetime, Timestamp])
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
def test_slice_datetime_locs(self, box, kind, tz_aware_fixture):
# GH 34077
Expand Down