Skip to content

Resolves Issue 21344: provide a timedelta in a non-string format #21444

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.23.1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,4 @@ Bug Fixes

- Tab completion on :class:`Index` in IPython no longer outputs deprecation warnings (:issue:`21125`)
- Bug preventing pandas being used on Windows without C++ redistributable installed (:issue:`21106`)
- Add `resolution_timedelta` to :class:`Timedelta` to get non-string representations of resolution (:issue: `21344`)
Copy link
Member

Choose a reason for hiding this comment

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

Could you move to v0.23.2.txt

32 changes: 31 additions & 1 deletion pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -795,7 +795,11 @@ cdef class _Timedelta(timedelta):

@property
def resolution(self):
""" return a string representing the lowest resolution that we have """
"""
Return a string representing the lowest resolution that we have.
Copy link
Member

Choose a reason for hiding this comment

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

Instead of lowest resolution that we have, I think I'd be clearer if it was lowest resolution of the Timedelta instance

Note that this is nonstandard behavior.
Copy link
Member

Choose a reason for hiding this comment

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

Instead, could you create a "See Also" section with the new method name, similar to this: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.isin.html

To retrieve a timedelta object use the resolution_timedelta property
"""

self._ensure_components()
if self._ns:
Expand All @@ -813,6 +817,32 @@ cdef class _Timedelta(timedelta):
else:
return "D"

@property
def resolution_timedelta(self):
Copy link
Member

Choose a reason for hiding this comment

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

The general idea was to have this new property return Timedelta(nanosecond=1) akin to how datetime.timedelta.resolution returns timedelta(microseconds=1)

"""
Return a timedelta object (rather than a string)
representing the lowest resolution we have.
Copy link
Member

Choose a reason for hiding this comment

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

Instead of lowest resolution that we have, I think I'd be clearer if it was lowest resolution of the Timedelta class

to retrieve a string use the resolution property.
Copy link
Member

Choose a reason for hiding this comment

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

Instead, could you create a "See Also" section with the new method name, similar to this: https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.isin.html

"""

self._ensure_components()
if self._ns:
# At time of writing datetime.timedelta doesn't
# support nanoseconds as a keyword argument.
return timedelta(microseconds=0.1)
elif self._us:
return timedelta(microseconds=1)
elif self._ms:
return timedelta(milliseconds=1)
elif self._s:
return timedelta(seconds=1)
elif self._m:
return timedelta(minutes=1)
elif self._h:
return timedelta(hours=1)
else:
return timedelta(days=1)

@property
def nanoseconds(self):
"""
Expand Down
28 changes: 28 additions & 0 deletions pandas/tests/scalar/timedelta/test_timedelta.py
Original file line number Diff line number Diff line change
Expand Up @@ -588,3 +588,31 @@ def test_components(self):
result = s.dt.components
assert not result.iloc[0].isna().all()
assert result.iloc[1].isna().all()

def test_resolution(self):
# GH 21344
assert Timedelta(nanoseconds=30).resolution == 'N'
# Note that datetime.timedelta doesn't offer
# finer resolution than microseconds
assert Timedelta(nanoseconds=30).resolution_timedelta.resolution == \
timedelta(0, 0, 1)

assert Timedelta(microseconds=30).resolution == 'U'
assert Timedelta(microseconds=30).resolution_timedelta.resolution == \
timedelta(0, 0, 1)

assert Timedelta(milliseconds=30).resolution == 'L'
assert Timedelta(milliseconds=30).resolution_timedelta.resolution == \
timedelta(0, 0, 1)

assert Timedelta(seconds=30).resolution == 'S'
assert Timedelta(seconds=30).resolution_timedelta.resolution == \
timedelta(0, 0, 1)

assert Timedelta(minutes=30).resolution == 'T'
assert Timedelta(minutes=30).resolution_timedelta.resolution == \
timedelta(0, 0, 1)

assert Timedelta(hours=2).resolution == 'H'
assert Timedelta(hours=2).resolution_timedelta.resolution == \
timedelta(0, 0, 1)