Skip to content
Open
Show file tree
Hide file tree
Changes from 4 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
6 changes: 0 additions & 6 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
no_default,
)
from pandas._libs.tslibs import (
OutOfBoundsDatetime,
Timestamp,
tz_compare,
)
Expand Down Expand Up @@ -6204,11 +6203,6 @@ def _maybe_downcast_for_indexing(self, other: Index) -> tuple[Index, Index]:
# standardize on UTC
return self.tz_convert("UTC"), other.tz_convert("UTC")

elif self.inferred_type == "date" and isinstance(other, ABCDatetimeIndex):
try:
return type(other)(self), other
except OutOfBoundsDatetime:
return self, other
elif self.inferred_type == "timedelta" and isinstance(other, ABCTimedeltaIndex):
# TODO: we dont have tests that get here
return type(other)(self), other
Expand Down
8 changes: 7 additions & 1 deletion pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,13 @@ def get_loc(self, key):
def _maybe_cast_slice_bound(self, label, side: str):
# GH#42855 handle date here instead of get_slice_bound
if isinstance(label, dt.date) and not isinstance(label, dt.datetime):
# Pandas supports slicing with dates, treated as datetimes at midnight.
warnings.warn(
Copy link
Member

Choose a reason for hiding this comment

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

i dont think this is part of the original issue?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thank you for the feedback. I have removed the unrelated parts of this issue, and I will look into it further as the checks are still failing.

"Indexing/slicing with datetime.date is deprecated and will be removed "
"in a future version of pandas. Please convert to pd.Timestamp or "
"datetime64[ns] before indexing.",
FutureWarning,
stacklevel=2,
)
# https://github.com/pandas-dev/pandas/issues/31501
label = Timestamp(label).to_pydatetime()

Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/frame/indexing/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -1880,6 +1880,17 @@ def test_add_new_column_infer_string():
tm.assert_frame_equal(df, expected)


def test_datetime_indexer_consistency_pyarrow_date32():
# GH#62158
ser = Series(["2016-01-01"], dtype="date32[pyarrow]")
ser3 = ser.astype("datetime64[ns]")
dti = Index(ser3)
# All should be consistent
assert dti.get_loc(ser[0]) == 0
tm.assert_numpy_array_equal(dti.get_indexer(ser.values), [0])
Copy link
Member

Choose a reason for hiding this comment

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

the second arg here should be an array, not list

tm.assert_numpy_array_equal(dti.get_indexer(ser.values.astype(object)), [0])


class TestSetitemValidation:
# This is adapted from pandas/tests/arrays/masked/test_indexing.py
def _check_setitem_invalid(self, df, invalid, indexer):
Expand Down
2 changes: 1 addition & 1 deletion pandas/tests/frame/methods/test_asfreq.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ def test_asfreq_with_date_object_index(self, frame_or_series):
ts = frame_or_series(np.random.default_rng(2).standard_normal(20), index=rng)

ts2 = ts.copy()
ts2.index = [x.date() for x in ts2.index]
ts2.index = to_datetime([x.date() for x in ts2.index])
Copy link
Member

Choose a reason for hiding this comment

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

i think this defeats the purpose of this test


result = ts2.asfreq("4h", method="ffill")
expected = ts.asfreq("4h", method="ffill")
Expand Down
15 changes: 10 additions & 5 deletions pandas/tests/indexes/datetimes/test_indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -562,17 +562,22 @@ def test_get_indexer(self):
idx.get_indexer(idx[[0]], method="nearest", tolerance="foo")

@pytest.mark.parametrize(
"target",
"target, expected",
[
[date(2020, 1, 1), Timestamp("2020-01-02")],
[Timestamp("2020-01-01"), date(2020, 1, 2)],
(
[date(2020, 1, 1), Timestamp("2020-01-02")],
np.array([-1, 1], dtype=np.intp),
),
(
[Timestamp("2020-01-01"), Timestamp(date(2020, 1, 2))],
np.array([0, 1], dtype=np.intp),
),
],
)
def test_get_indexer_mixed_dtypes(self, target):
def test_get_indexer_mixed_dtypes(self, target, expected):
# https://github.com/pandas-dev/pandas/issues/33741
values = DatetimeIndex([Timestamp("2020-01-01"), Timestamp("2020-01-02")])
result = values.get_indexer(target)
expected = np.array([0, 1], dtype=np.intp)
tm.assert_numpy_array_equal(result, expected)

@pytest.mark.parametrize(
Expand Down
3 changes: 2 additions & 1 deletion pandas/tests/series/test_arithmetic.py
Original file line number Diff line number Diff line change
Expand Up @@ -763,7 +763,8 @@ def test_align_date_objects_with_datetimeindex(self):

ts_slice = ts[5:]
ts2 = ts_slice.copy()
ts2.index = [x.date() for x in ts2.index]
# Explicitly convert date objects to Timestamps for alignment
ts2.index = [pd.Timestamp(x.date()) for x in ts2.index]

result = ts + ts2
result2 = ts2 + ts
Expand Down
Loading