Skip to content

BUG: Index.get_slice_bounds does not accept datetime.date or tz naive datetime.datetimes #35848

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 18 commits into from
Sep 2, 2020
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
3 changes: 2 additions & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,8 @@ Datetimelike
- Bug in :attr:`DatetimeArray.date` where a ``ValueError`` would be raised with a read-only backing array (:issue:`33530`)
- Bug in ``NaT`` comparisons failing to raise ``TypeError`` on invalid inequality comparisons (:issue:`35046`)
- Bug in :class:`DateOffset` where attributes reconstructed from pickle files differ from original objects when input values exceed normal ranges (e.g months=12) (:issue:`34511`)
-
- Bug in :meth:`DatetimeIndex.get_slice_bound` where ``datetime.date`` objects were not accepted or naive :class:`Timestamp` with a tz-aware :class:`DatetimeIndex` (:issue:`35690`)
- Bug in :meth:`DatetimeIndex.slice_locs` where ``datetime.date`` objects were not accepted (:issue:`34077`)

Timedelta
^^^^^^^^^
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,7 @@ def get_loc(self, key, method=None, tolerance=None):
raise KeyError(orig_key) from err

def _maybe_cast_for_get_loc(self, key) -> Timestamp:
# needed to localize naive datetimes
# needed to localize naive datetimes or dates (GH 35690)
key = Timestamp(key)
if key.tzinfo is None:
key = key.tz_localize(self.tz)
Expand Down Expand Up @@ -677,8 +677,7 @@ def _maybe_cast_slice_bound(self, label, side: str, kind):
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:
return label
return self._maybe_cast_for_get_loc(label)

def _get_string_slice(self, key: str, use_lhs: bool = True, use_rhs: bool = True):
freq = getattr(self, "freqstr", getattr(self, "inferred_freq", None))
Expand Down
26 changes: 26 additions & 0 deletions pandas/tests/indexes/base_class/test_indexing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import pytest

from pandas import Index


class TestGetSliceBounds:
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
@pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)])
def test_get_slice_bounds_within(self, kind, side, expected):
index = Index(list("abcdef"))
result = index.get_slice_bound("e", kind=kind, side=side)
assert result == expected

@pytest.mark.parametrize("kind", ["getitem", "loc", None])
@pytest.mark.parametrize("side", ["left", "right"])
@pytest.mark.parametrize(
"data, bound, expected", [(list("abcdef"), "x", 6), (list("bcdefg"), "a", 0)],
)
def test_get_slice_bounds_outside(self, kind, side, expected, data, bound):
index = Index(data)
result = index.get_slice_bound(bound, kind=kind, side=side)
assert result == expected

def test_get_slice_bounds_invalid_side(self):
with pytest.raises(ValueError, match="Invalid value for side kwarg"):
Index([]).get_slice_bound("a", kind=None, side="middle")
42 changes: 41 additions & 1 deletion pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pandas.errors import InvalidIndexError

import pandas as pd
from pandas import DatetimeIndex, Index, Timestamp, date_range, notna
from pandas import DatetimeIndex, Index, Timestamp, bdate_range, date_range, notna
import pandas._testing as tm

from pandas.tseries.offsets import BDay, CDay
Expand Down Expand Up @@ -665,3 +665,43 @@ def test_get_value(self):
with tm.assert_produces_warning(FutureWarning):
result = dti.get_value(ser, key.to_datetime64())
assert result == 7


class TestGetSliceBounds:
@pytest.mark.parametrize("box", [date, datetime, Timestamp])
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
@pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)])
def test_get_slice_bounds_datetime_within(
self, box, kind, side, expected, tz_aware_fixture
):
# GH 35690
index = bdate_range("2000-01-03", "2000-02-11").tz_localize(tz_aware_fixture)
result = index.get_slice_bound(
box(year=2000, month=1, day=7), kind=kind, side=side
)
assert result == expected

@pytest.mark.parametrize("box", [date, datetime, Timestamp])
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
@pytest.mark.parametrize("side", ["left", "right"])
@pytest.mark.parametrize("year, expected", [(1999, 0), (2020, 30)])
def test_get_slice_bounds_datetime_outside(
self, box, kind, side, year, expected, tz_aware_fixture
):
# GH 35690
index = bdate_range("2000-01-03", "2000-02-11").tz_localize(tz_aware_fixture)
result = index.get_slice_bound(
box(year=year, month=1, day=7), kind=kind, side=side
)
assert result == expected

@pytest.mark.parametrize("box", [date, datetime, Timestamp])
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
def test_slice_datetime_locs(self, box, kind, tz_aware_fixture):
# GH 34077
index = DatetimeIndex(["2010-01-01", "2010-01-03"]).tz_localize(
tz_aware_fixture
)
result = index.slice_locs(box(2010, 1, 1), box(2010, 1, 2))
expected = (0, 1)
assert result == expected
19 changes: 19 additions & 0 deletions pandas/tests/indexes/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -679,3 +679,22 @@ def test_float64_index_difference():

result = string_index.difference(float_index)
tm.assert_index_equal(result, string_index)


class TestGetSliceBounds:
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
@pytest.mark.parametrize("side, expected", [("left", 4), ("right", 5)])
def test_get_slice_bounds_within(self, kind, side, expected):
index = Index(range(6))
result = index.get_slice_bound(4, kind=kind, side=side)
assert result == expected

@pytest.mark.parametrize("kind", ["getitem", "loc", None])
@pytest.mark.parametrize("side", ["left", "right"])
@pytest.mark.parametrize(
"bound, expected", [(-1, 0), (10, 6)],
)
def test_get_slice_bounds_outside(self, kind, side, expected, bound):
index = Index(range(6))
result = index.get_slice_bound(bound, kind=kind, side=side)
assert result == expected