Skip to content

DatetimeIndex get_loc validates date object #35478

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 1 commit 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
4 changes: 2 additions & 2 deletions pandas/core/arrays/datetimes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, time, timedelta, tzinfo
from datetime import date, datetime, time, timedelta, tzinfo
from typing import Optional, Union
import warnings

Expand Down Expand Up @@ -152,7 +152,7 @@ class DatetimeArray(dtl.TimelikeOps, dtl.DatelikeOps):

_typ = "datetimearray"
_scalar_type = Timestamp
_recognized_scalars = (datetime, np.datetime64)
_recognized_scalars = (datetime, np.datetime64, date)
Copy link
Member

Choose a reason for hiding this comment

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

this is the right way to address #35466, but you also need to look at the other places where _recognized_scalars is used and make sure those tests are updated appropriately

_is_recognized_dtype = is_datetime64_any_dtype

# define my properties & methods for delegation
Expand Down
4 changes: 3 additions & 1 deletion pandas/core/internals/blocks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from datetime import datetime, timedelta
from datetime import date, datetime, timedelta
import inspect
import re
from typing import TYPE_CHECKING, Any, List, Optional, Type, Union, cast
Expand Down Expand Up @@ -2226,6 +2226,8 @@ def _can_hold_element(self, element: Any) -> bool:
return is_datetime64_dtype(tipo)
elif element is NaT:
return True
elif isinstance(element, date):
return True
elif isinstance(element, datetime):
if self.is_datetimetz:
return tz_compare(element.tzinfo, self.dtype.tz)
Expand Down
4 changes: 0 additions & 4 deletions pandas/tests/arithmetic/test_datetime64.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ def test_compare_zerodim(self, tz_naive_fixture, box_with_array):
4.0,
object(),
timedelta(days=2),
# GH#19800, GH#19301 datetime.date comparison raises to
# match DatetimeIndex/Timestamp. This also matches the behavior
# of stdlib datetime.datetime
datetime(2001, 1, 1).date(),
# GH#19301 None and NaN are *not* cast to NaT for comparisons
None,
np.nan,
Expand Down
7 changes: 7 additions & 0 deletions pandas/tests/indexes/datetimes/test_datetime.py
Original file line number Diff line number Diff line change
Expand Up @@ -403,3 +403,10 @@ def test_split_non_utc(self):
result = np.split(indices, indices_or_sections=[])[0]
expected = indices._with_freq(None)
tm.assert_index_equal(result, expected)

def test_in_contains_date_object(self):
# GH#35466
d1 = date(2002, 9, 1)
idx1 = DatetimeIndex([d1])
assert d1 in idx1
assert "2002-09-01" in idx1
2 changes: 1 addition & 1 deletion pandas/tests/internals/test_internals.py
Original file line number Diff line number Diff line change
Expand Up @@ -1067,7 +1067,7 @@ def test_datetime_block_can_hold_element(self):
arr[0] = val

val = date(2010, 10, 10)
assert not block._can_hold_element(val)
assert block._can_hold_element(val)

msg = (
"value should be a 'Timestamp', 'NaT', "
Expand Down