Skip to content

BUG: #35830 Restore index & slice by date to DatetimeIndex #39380

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

Closed
wants to merge 3 commits into from
Closed
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
7 changes: 6 additions & 1 deletion pandas/core/arrays/datetimelike.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from datetime import datetime, timedelta
from datetime import date, datetime, time, timedelta
import operator
from typing import (
TYPE_CHECKING,
Expand Down Expand Up @@ -552,6 +552,11 @@ def _validate_scalar(
-------
self._scalar_type or NaT
"""

if type(value) == date:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this (and the similar test below) be isinstance(value, date)?

# GH35830
value = datetime.combine(value, time.min)

if isinstance(value, str):
# NB: Careful about tzawareness
try:
Expand Down
4 changes: 4 additions & 0 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,10 @@ def get_loc(self, key, method=None, tolerance=None):
if is_valid_nat_for_dtype(key, self.dtype):
key = NaT

if type(key) == date:
# GH35830
key = datetime.combine(key, time.min)

if isinstance(key, self._data._recognized_scalars):
# needed to localize naive datetimes
self._deprecate_mismatched_indexing(key)
Expand Down
12 changes: 11 additions & 1 deletion pandas/tests/indexing/test_loc.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
""" test label based indexing with loc """
from datetime import datetime, time, timedelta
from datetime import date, datetime, time, timedelta
from io import StringIO
import re

Expand Down Expand Up @@ -2130,3 +2130,13 @@ def test_loc_iloc_setitem_with_listlike(self, size, array_fn):
ser = Series(0, index=list("abcde"), dtype=object)
ser.iloc[0] = arr
tm.assert_series_equal(ser, expected)

def test_loc_getitem_date_objs_with_datetimeindex(self):
# GH35830
dates = [date(2000, 1, i) for i in (1, 2, 5)]
values = [1, 2, 3]
s = Series(values, dates)
i = 1
expected = values[i]
result = s.loc[dates[i]]
assert result == expected