Skip to content

Commit b95ef5d

Browse files
jbrockmendelJustine Wezenaar
authored andcommitted
DEPR: slicing with a date object (pandas-dev#62428)
1 parent ccb5286 commit b95ef5d

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

doc/source/whatsnew/v3.0.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -666,6 +666,7 @@ Other Deprecations
666666
- Deprecated using ``epoch`` date format in :meth:`DataFrame.to_json` and :meth:`Series.to_json`, use ``iso`` instead. (:issue:`57063`)
667667
- Deprecated allowing ``fill_value`` that cannot be held in the original dtype (excepting NA values for integer and bool dtypes) in :meth:`Series.unstack` and :meth:`DataFrame.unstack` (:issue:`12189`, :issue:`53868`)
668668
- Deprecated allowing ``fill_value`` that cannot be held in the original dtype (excepting NA values for integer and bool dtypes) in :meth:`Series.shift` and :meth:`DataFrame.shift` (:issue:`53802`)
669+
- Deprecated slicing on a :class:`Series` or :class:`DataFrame` with a :class:`DatetimeIndex` using a ``datetime.date`` object, explicitly cast to :class:`Timestamp` instead (:issue:`35830`)
669670

670671
.. ---------------------------------------------------------------------------
671672
.. _whatsnew_300.prior_deprecations:

pandas/core/indexes/datetimes.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@
2626
to_offset,
2727
)
2828
from pandas._libs.tslibs.offsets import prefix_mapping
29+
from pandas.errors import Pandas4Warning
2930
from pandas.util._decorators import (
3031
cache_readonly,
3132
doc,
3233
set_module,
3334
)
35+
from pandas.util._exceptions import find_stack_level
3436

3537
from pandas.core.dtypes.common import is_scalar
3638
from pandas.core.dtypes.dtypes import (
@@ -645,6 +647,13 @@ def _maybe_cast_slice_bound(self, label, side: str):
645647
# Pandas supports slicing with dates, treated as datetimes at midnight.
646648
# https://github.com/pandas-dev/pandas/issues/31501
647649
label = Timestamp(label).to_pydatetime()
650+
warnings.warn(
651+
# GH#35830 deprecate last remaining inconsistent date treatment
652+
"Slicing with a datetime.date object is deprecated. "
653+
"Explicitly cast to Timestamp instead.",
654+
Pandas4Warning,
655+
stacklevel=find_stack_level(),
656+
)
648657

649658
label = super()._maybe_cast_slice_bound(label, side)
650659
self._data._assert_tzawareness_compat(label)

pandas/tests/indexes/datetimes/test_indexing.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
from pandas._libs import index as libindex
1212
from pandas.compat.numpy import np_long
13+
from pandas.errors import Pandas4Warning
1314
import pandas.util._test_decorators as td
1415

1516
import pandas as pd
@@ -654,13 +655,16 @@ def test_get_slice_bounds_datetime_within(
654655
index = bdate_range("2000-01-03", "2000-02-11").tz_localize(tz)
655656
key = box(year=2000, month=1, day=7)
656657

657-
if tz is not None:
658-
with pytest.raises(TypeError, match="Cannot compare tz-naive"):
659-
# GH#36148 we require tzawareness-compat as of 2.0
660-
index.get_slice_bound(key, side=side)
661-
else:
662-
result = index.get_slice_bound(key, side=side)
663-
assert result == expected
658+
warn = None if box is not date else Pandas4Warning
659+
msg = "Slicing with a datetime.date object is deprecated"
660+
with tm.assert_produces_warning(warn, match=msg):
661+
if tz is not None:
662+
with pytest.raises(TypeError, match="Cannot compare tz-naive"):
663+
# GH#36148 we require tzawareness-compat as of 2.0
664+
index.get_slice_bound(key, side=side)
665+
else:
666+
result = index.get_slice_bound(key, side=side)
667+
assert result == expected
664668

665669
@pytest.mark.parametrize("box", [datetime, Timestamp])
666670
@pytest.mark.parametrize("side", ["left", "right"])

pandas/tests/series/indexing/test_getitem.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
timezones,
1717
)
1818
from pandas.compat.numpy import np_version_gt2
19+
from pandas.errors import Pandas4Warning
1920

2021
from pandas.core.dtypes.common import is_scalar
2122

@@ -314,7 +315,9 @@ def test_getitem_slice_date(self, slc, positions):
314315
[0, 1, 2],
315316
DatetimeIndex(["2019-01-01", "2019-01-01T06:00:00", "2019-01-02"]),
316317
)
317-
result = ser[slc]
318+
msg = "Slicing with a datetime.date object is deprecated"
319+
with tm.assert_produces_warning(Pandas4Warning, match=msg):
320+
result = ser[slc]
318321
expected = ser.take(positions)
319322
tm.assert_series_equal(result, expected)
320323

0 commit comments

Comments
 (0)