Skip to content

Docstring changes to pandas.Series.dt.to_pydatetime #20198

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
Changes from 10 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
42 changes: 42 additions & 0 deletions pandas/core/indexes/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,48 @@ class DatetimeProperties(Properties):
"""

def to_pydatetime(self):
"""
Return an ndarray of native Python datetime objects.
Copy link
Member

Choose a reason for hiding this comment

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

I'd use something like "Return the data as native Python datetime objects".

The context helps, but the way is written makes me feel more like it's returning an empty or random array, not transforming data.


Timezone information is retained if present.

.. warning::

Python's datetimes use microsecond resolution, which is lower than
pandas' (nanosecond). The values are truncated.

Returns
-------
numpy.ndarray
object dtype array containing native Python datetime objects.

See Also
--------
datetime.datetime : Standard library value for a datetime.

Examples
--------
>>> s = pd.Series(pd.date_range('20180310', periods=2))
>>> s.head()
Copy link
Member

Choose a reason for hiding this comment

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

head can be removed

0 2018-03-10
1 2018-03-11
dtype: datetime64[ns]
Copy link
Member

Choose a reason for hiding this comment

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

Unnecessary head().

Copy link
Contributor Author

@priya-gitTest priya-gitTest Mar 11, 2018

Choose a reason for hiding this comment

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

Hi @datapythonista ,
If I remove the head, the validation test fails.
Line 18, in pandas.Series.dt.to_pydatetime
Failed example:
s = pd.Series(pd.date_range('20180310', periods=2))
Expected:
0 2018-03-10
1 2018-03-11
dtype: datetime64[ns]
Got nothing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

fixed it by just using >>> s

Copy link
Contributor

Choose a reason for hiding this comment

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

You can just do s by itself.


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

pandas' nanosecond precision is truncated to microseconds.

>>> idx = pd.date_range('2017', periods=2, freq='ns')
Copy link
Member

Choose a reason for hiding this comment

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

can you use a Series example here?

Copy link
Contributor Author

@priya-gitTest priya-gitTest Mar 13, 2018

Choose a reason for hiding this comment

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

nanosec
Btw, I dont get the same output as you @TomAugspurger for the nanosecond-precision data in the jupyter notebook.
I run my code on Windows !
Strangely the docstring Validation test passes, so it must be going right.

Copy link
Contributor

Choose a reason for hiding this comment

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

Strange, not sure why that would be.

Are you able to wrap that in a Series and do ser.dt.to_pydatetimte()? I'll it once you update.

>>> idx
DatetimeIndex(['2017-01-01 00:00:00', '2017-01-01 00:00:00.000000001'],
dtype='datetime64[ns]', freq='N')

>>> idx.to_pydatetime()
array([datetime.datetime(2017, 1, 1, 0, 0),
datetime.datetime(2017, 1, 1, 0, 0)], dtype=object)
"""
Copy link
Contributor

Choose a reason for hiding this comment

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

I gave up on a tz example since the repr for datetime w/ tz is so long. Wasn't able to make it look nice.

return self._get_values().to_pydatetime()

@property
Expand Down