Skip to content

CLN/depr: dt.to_pydatetime #52803

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

Merged
merged 2 commits into from
Apr 22, 2023
Merged
Changes from 1 commit
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
12 changes: 9 additions & 3 deletions pandas/core/indexes/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,12 @@ def to_pydatetime(self) -> np.ndarray:
"""
Return the data as an array of :class:`datetime.datetime` objects.

.. deprecated:: 2.1.0

The current behavior of dt.to_pydatetime is deprecated.
In a future version this will return a Series containing python
datetime objects instead of a ndarray.

Timezone information is retained if present.

.. warning::
Expand All @@ -328,19 +334,19 @@ def to_pydatetime(self) -> np.ndarray:
1 2018-03-11
dtype: datetime64[ns]

>>> s.dt.to_pydatetime()
>>> s.dt.to_pydatetime() # doctest: +SKIP
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 I would prefer to keep these examples running, so that when we change the behavior we are reminded to change these examples

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok, I can see that point. Keep the deprecation notice?

Copy link
Member

Choose a reason for hiding this comment

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

Keep the deprecation notice?

Yes definitely

array([datetime.datetime(2018, 3, 10, 0, 0),
datetime.datetime(2018, 3, 11, 0, 0)], dtype=object)

pandas' nanosecond precision is truncated to microseconds.

>>> s = pd.Series(pd.date_range('20180310', periods=2, freq='ns'))
>>> s
>>> s # doctest: +SKIP
0 2018-03-10 00:00:00.000000000
1 2018-03-10 00:00:00.000000001
dtype: datetime64[ns]

>>> s.dt.to_pydatetime()
>>> s.dt.to_pydatetime() # doctest: +SKIP
array([datetime.datetime(2018, 3, 10, 0, 0),
datetime.datetime(2018, 3, 10, 0, 0)], dtype=object)
"""
Expand Down