Skip to content
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

DOC: Improved the docstring of pandas.Series.dt.to_pytimedelta #20142

Merged
Merged
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
33 changes: 33 additions & 0 deletions pandas/core/indexes/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,39 @@ class TimedeltaProperties(Properties):
"""

def to_pytimedelta(self):
"""
Return array of Timedeltas as `datetime.timedelta`.

Python's standard `datetime` library uses a different representation to
what pandas for differences between times. This method converts a
Series of pandas Timedeltas to `datetime.timedelta` format with
the same length as the original Series.

Returns
-------
a : numpy.ndarray
1D array containing data with `datetime.timedelta` type.

Examples
--------
>>> s = pd.Series(pd.to_timedelta(np.arange(5), unit='d'))
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need import datetime here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't seem to need the import, but to clarify things, I've added more to the description.

>>> s
0 0 days
1 1 days
2 2 days
3 3 days
4 4 days
dtype: timedelta64[ns]

>>> s.dt.to_pytimedelta()
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add a blank line before this case

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Added.

array([datetime.timedelta(0), datetime.timedelta(1),
datetime.timedelta(2), datetime.timedelta(3),
datetime.timedelta(4)], dtype=object)

See Also
--------
datetime.timedelta
"""
return self._get_values().to_pytimedelta()

@property
Expand Down