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 8 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
37 changes: 37 additions & 0 deletions pandas/core/indexes/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,43 @@ 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.

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

Examples
--------
This method is available on both Series with datetime values, under the
``.dt`` accessor, and directly on DatetimeIndex.

**Series**

>>> 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)

**DatetimeIndex**
>>> idx = pd.date_range("2018-03-10", periods=2)
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 leave a blank line between the title and the code, as you did with Series.

Copy link
Member

Choose a reason for hiding this comment

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

I actually don't think we need a DatetimeIndex example? As this are the docs specific to Series.dt.to_pydatetime (they don't share their docstring)

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, my apologies. I didn't realize they don't share.

Copy link
Member

Choose a reason for hiding this comment

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

Opened #20306 for it

>>> idx # doctest: +NORMALIZE_WHITESPACE
DatetimeIndex(['2018-03-10', '2018-03-11'],
dtype='datetime64[ns]', freq='D')

>>> idx.to_pydatetime()
array([datetime.datetime(2018, 3, 10, 0, 0),
datetime.datetime(2018, 3, 11, 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