Skip to content

Conversation

Aniketsy
Copy link
Contributor

This PR resolves (GH#62158) by removing the legacy special-casing for Python date objects in DatetimeIndex indexing logic.

What’s Changed:
Removed the special-case handling of date objects in DatetimeIndex.getitem.

Updated/added tests in pandas/tests/frame/indexing/test_indexing.py to reflect the new, consistent behavior.

Please let me know if my approach or fix needs any improvements . I’m open to feedback and happy to make changes based on suggestions.
Thankyou !

@Aniketsy
Copy link
Contributor Author

@jbrockmendel hi
Could you please review my PR for the datetime/date indexing deprecation?

I have a question about handling the new deprecation warning
Do I need to add @pytest.mark.filterwarnings("ignore:Indexing/slicing with datetime.date is deprecated") to every individual test that triggers this warning, or is there a preferred way to handle this globally for the test suite?
Also should I update every test that uses the deprecated behavior, or is there a more efficient approach?

Thanks!

# 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.

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


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

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]
Copy link
Member

Choose a reason for hiding this comment

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

seems like this defeats the purpose of the test


expected_inner = DataFrame(
[[pd.Timestamp("2001-01-01").date(), 1.1, 1.3]],
[[date(2001, 1, 1), 1.1, 1.3]],
Copy link
Member

Choose a reason for hiding this comment

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

edits in this file look unnecessary

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks , I will remove changes of this file.


class TestGetIndexer:
def test_get_indexer_date_objs(self):
rng = date_range("1/1/2000", periods=20)
Copy link
Member

Choose a reason for hiding this comment

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

comment here pointing back to the issue saying the behavior changed

@jbrockmendel
Copy link
Member

Mostly looks good, will need a whatsnew note

@Aniketsy
Copy link
Contributor Author

@jbrockmendel Thanks for the feedback! I’ve applied the suggested changes and updated the PR. Please let me know if there’s anything else that needs adjustment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants