Skip to content

Commit 69ae807

Browse files
committed
BUG: Remove special-casing for date objects in DatetimeIndex indexing (GH#62158)
1 parent 2f98bd4 commit 69ae807

File tree

4 files changed

+20
-8
lines changed

4 files changed

+20
-8
lines changed

pandas/core/indexes/datetimes.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,13 @@ def get_loc(self, key):
629629
def _maybe_cast_slice_bound(self, label, side: str):
630630
# GH#42855 handle date here instead of get_slice_bound
631631
if isinstance(label, dt.date) and not isinstance(label, dt.datetime):
632-
# Pandas supports slicing with dates, treated as datetimes at midnight.
632+
warnings.warn(
633+
"Indexing/slicing with datetime.date is deprecated and will be removed "
634+
"in a future version of pandas. Please convert to pd.Timestamp or "
635+
"datetime64[ns] before indexing.",
636+
FutureWarning,
637+
stacklevel=2,
638+
)
633639
# https://github.com/pandas-dev/pandas/issues/31501
634640
label = Timestamp(label).to_pydatetime()
635641

pandas/tests/frame/methods/test_asfreq.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,7 @@ def test_asfreq_with_date_object_index(self, frame_or_series):
190190
ts = frame_or_series(np.random.default_rng(2).standard_normal(20), index=rng)
191191

192192
ts2 = ts.copy()
193-
ts2.index = [x.date() for x in ts2.index]
193+
ts2.index = to_datetime([x.date() for x in ts2.index])
194194

195195
result = ts2.asfreq("4h", method="ffill")
196196
expected = ts.asfreq("4h", method="ffill")

pandas/tests/indexes/datetimes/test_indexing.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -562,17 +562,22 @@ def test_get_indexer(self):
562562
idx.get_indexer(idx[[0]], method="nearest", tolerance="foo")
563563

564564
@pytest.mark.parametrize(
565-
"target",
565+
"target, expected",
566566
[
567-
[date(2020, 1, 1), Timestamp("2020-01-02")],
568-
[Timestamp("2020-01-01"), date(2020, 1, 2)],
567+
(
568+
[date(2020, 1, 1), Timestamp("2020-01-02")],
569+
np.array([-1, 1], dtype=np.intp),
570+
),
571+
(
572+
[Timestamp("2020-01-01"), Timestamp(date(2020, 1, 2))],
573+
np.array([0, 1], dtype=np.intp),
574+
),
569575
],
570576
)
571-
def test_get_indexer_mixed_dtypes(self, target):
577+
def test_get_indexer_mixed_dtypes(self, target, expected):
572578
# https://github.com/pandas-dev/pandas/issues/33741
573579
values = DatetimeIndex([Timestamp("2020-01-01"), Timestamp("2020-01-02")])
574580
result = values.get_indexer(target)
575-
expected = np.array([0, 1], dtype=np.intp)
576581
tm.assert_numpy_array_equal(result, expected)
577582

578583
@pytest.mark.parametrize(

pandas/tests/series/test_arithmetic.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,8 @@ def test_align_date_objects_with_datetimeindex(self):
763763

764764
ts_slice = ts[5:]
765765
ts2 = ts_slice.copy()
766-
ts2.index = [x.date() for x in ts2.index]
766+
# Explicitly convert date objects to Timestamps for alignment
767+
ts2.index = [pd.Timestamp(x.date()) for x in ts2.index]
767768

768769
result = ts + ts2
769770
result2 = ts2 + ts

0 commit comments

Comments
 (0)