8000 DEPR: disallow tznaive datetimes when indexing tzaware datetimeindex by jbrockmendel · Pull Request #36148 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

DEPR: disallow tznaive datetimes when indexing tzaware datetimeindex #36148

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 14 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
deprecate wrong behavior
  • Loading branch information
jbrockmendel committed Oct 1, 2020
commit 4789e635900981d8c805b588b68c047610fb963c
33 changes: 24 additions & 9 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,28 @@ def _validate_partial_date_slice(self, reso: Resolution):
# _parsed_string_to_bounds allows it.
raise KeyError

def _deprecate_mismatched_indexing(self, key):
# GH#36148
# we get here with isinstance(key, self._data._recognized_scalars)
try:
self._data._assert_tzawareness_compat(key)
except TypeError:
if self.tz is None:
msg = (
"Indexing a timezone-naive DatetimeIndex with a "
"timezone-aware datetime is deprecated and will "
"raise KeyError in a future version. "
"Use a timezone-naive object instead."
)
else:
msg = (
"Indexing a timezone-aware DatetimeIndex with a "
"timezone-naive datetime is deprecated and will "
"raise KeyError in a future version. "
"Use a timezone-aware object instead."
)
warnings.warn(msg, FutureWarning, stacklevel=5)

def get_loc(self, key, method=None, tolerance=None):
"""
Get integer location for requested label
Expand All @@ -617,10 +639,7 @@ def get_loc(self, key, method=None, tolerance=None):

if isinstance(key, self._data._recognized_scalars):
# needed to localize naive datetimes
try:
self._data._assert_tzawareness_compat(key)
except TypeError as err:
raise KeyError(key) from err
self._deprecate_mismatched_indexing(key)
key = self._maybe_cast_for_get_loc(key)

elif isinstance(key, str):
Expand Down Expand Up @@ -679,10 +698,6 @@ def _maybe_cast_slice_bound(self, label, side: str, kind):
-------
label : object

Raises
------
TypeError : indexing timezone-aware DatetimeIndex with tz-naive datetime

Notes
-----
Value of `side` parameter should be validated in caller.
Expand All @@ -707,7 +722,7 @@ def _maybe_cast_slice_bound(self, label, side: str, kind):
return upper if side == "left" else lower
return lower if side == "left" else upper
elif isinstance(label, (self._data._recognized_scalars, date)):
self._data._assert_tzawareness_compat(label)
self._deprecate_mismatched_indexing(label)
return self._maybe_cast_for_get_loc(label)

def _get_string_slice(self, key: str, use_lhs: bool = True, use_rhs: bool = True):
Expand Down
38 changes: 16 additions & 22 deletions pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,14 +678,12 @@ def test_get_slice_bounds_datetime_within(
tz = tz_aware_fixture
index = bdate_range("2000-01-03", "2000-02-11").tz_localize(tz)
key = box(year=2000, month=1, day=7)
if tz is None:

warn = None if tz is None else FutureWarning
with tm.assert_produces_warning(warn, check_stacklevel=False):
# GH#36148 will require tzawareness-compat
result = index.get_slice_bound(key, kind=kind, side=side)
assert result == expected
else:
# We require tzawareness-compat in indexing
msg = "Cannot compare tz-naive and tz-aware datetime-like objects"
with pytest.raises(TypeError, match=msg):
index.get_slice_bound(key, kind=kind, side=side)
assert result == expected

@pytest.mark.parametrize("box", [date, datetime, Timestamp])
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
Expand All @@ -698,14 +696,12 @@ def test_get_slice_bounds_datetime_outside(
tz = tz_aware_fixture
index = bdate_range("2000-01-03", "2000-02-11").tz_localize(tz)
key = box(year=year, month=1, day=7)
if tz is None:

warn = None if tz is None else FutureWarning
with tm.assert_produces_warning(warn, check_stacklevel=False):
# GH#36148 will require tzawareness-compat
result = index.get_slice_bound(key, kind=kind, side=side)
assert result == expected
else:
# We require tzawareness compat in indexing
msg = "Cannot compare tz-naive and tz-aware datetime-like objects"
with pytest.raises(TypeError, match=msg):
index.get_slice_bound(key, kind=kind, side=side)
assert result == expected

@pytest.mark.parametrize("box", [date, datetime, Timestamp])
@pytest.mark.parametrize("kind", ["getitem", "loc", None])
Expand All @@ -714,12 +710,10 @@ def test_slice_datetime_locs(self, box, kind, tz_aware_fixture):
tz = tz_aware_fixture
index = DatetimeIndex(["2010-01-01", "2010-01-03"]).tz_localize(tz)
key = box(2010, 1, 1)
if tz is None:

warn = None if tz is None else FutureWarning
with tm.assert_produces_warning(warn, check_stacklevel=False):
# GH#36148 will require tzawareness-compat
result = index.slice_locs(key, box(2010, 1, 2))
expected = (0, 1)
assert result == expected
else:
# We require tzawareness-compat in indexing
msg = "Cannot compare tz-naive and tz-aware datetime-like objects"
with pytest.raises(TypeError, match=msg):
index.slice_locs(key, box(2010, 1, 2))
expected = (0, 1)
assert result == expected
28 changes: 20 additions & 8 deletions pandas/tests/series/indexing/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -241,23 +241,35 @@ def test_getitem_setitem_datetimeindex():
# TODO: do the same with Timestamps and dt64
msg = "Cannot compare tz-naive and tz-aware datetime-like objects"
naive = datetime(1990, 1, 1, 4)
with pytest.raises(KeyError, match=re.escape(repr(naive))):
ts[naive]
with tm.assert_produces_warning(FutureWarning):
# GH#36148 will require tzawareness compat
result = ts[naive]
expected = ts[4]
assert result == expected

result = ts.copy()
with pytest.raises(TypeError, match=msg):
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
# GH#36148 will require tzawareness compat
result[datetime(1990, 1, 1, 4)] = 0
with pytest.raises(TypeError, match=msg):
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
# GH#36148 will require tzawareness compat
result[datetime(1990, 1, 1, 4)] = ts[4]
tm.assert_series_equal(result, ts)

with pytest.raises(TypeError, match=msg):
ts[datetime(1990, 1, 1, 4) : datetime(1990, 1, 1, 7)]
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
# GH#36148 will require tzawareness compat
result = ts[datetime(1990, 1, 1, 4) : datetime(1990, 1, 1, 7)]
expected = ts[4:8]
tm.assert_series_equal(result, expected)

result = ts.copy()
with pytest.raises(TypeError, match=msg):
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
# GH#36148 will require tzawareness compat
result[datetime(1990, 1, 1, 4) : datetime(1990, 1, 1, 7)] = 0
with pytest.raises(TypeError, match=msg):
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
# GH#36148 will require tzawareness compat
result[datetime(1990, 1, 1, 4) : datetime(1990, 1, 1, 7)] = ts[4:8]
tm.assert_series_equal(result, ts)

lb = datetime(1990, 1, 1, 4)
rb = datetime(1990, 1, 1, 7)
Expand Down
4 changes: 2 additions & 2 deletions pandas/tests/series/methods/test_truncate.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,8 @@ def test_truncate_datetimeindex_tz(self):
# GH 9243
idx = date_range("4/1/2005", "4/30/2005", freq="D", tz="US/Pacific")
s = Series(range(len(idx)), index=idx)
msg = "Cannot compare tz-naive and tz-aware datetime-like objects"
with pytest.raises(TypeError, match=msg):
with tm.assert_produces_warning(FutureWarning, check_stacklevel=False):
# GH#36148 in the future will require tzawareness compat
s.truncate(datetime(2005, 4, 2), datetime(2005, 4, 4))

lb = idx[1]
Expand Down
0