Skip to content

ENH: Implement round dunder methods for Timestamp and Timedelta #58081

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/v3.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Other enhancements
- Support passing a :class:`Series` input to :func:`json_normalize` that retains the :class:`Series` :class:`Index` (:issue:`51452`)
- Users can globally disable any ``PerformanceWarning`` by setting the option ``mode.performance_warnings`` to ``False`` (:issue:`56920`)
- :meth:`Styler.format_index_names` can now be used to format the index and column names (:issue:`48936` and :issue:`47489`)
- Implemented built-in round for :class:`Timestamp` and :class:`Timedelta` such that ``round(Timestamp)`` and ``round(Timedelta)`` now work. (:issue:`58071`)
-

.. ---------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions pandas/_libs/tslibs/timedeltas.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2290,6 +2290,9 @@ class Timedelta(_Timedelta):
div = other // self
return div, other - div * self

def __round__(self, freq):
Copy link
Member

Choose a reason for hiding this comment

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

Doesn’t the dunder method not take a freq keyword?

Copy link
Member Author

Choose a reason for hiding this comment

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

You're right it doesn't, it instead takes ndigits which is not relevant here, so I thought for extra customizability I'll take freq in place of ndigits (same as NDFrame.__round__ which takes decimals instead of ndigits)

Copy link
Member Author

Choose a reason for hiding this comment

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

But I have 1 question, should freq have a default value? as you would usually expect round(object) to work without any extra arguments, if so, what should the default value be?

Copy link
Member

Choose a reason for hiding this comment

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

I would not expect that to work

Copy link
Member Author

Choose a reason for hiding this comment

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

Weirdly enough it does, for example:

In [1]: import pandas as pd
In [2]: td = pd.Timedelta(days=1, hours=2, minutes=34, seconds=56.78)
In [3]: round(td, "s")
Out[3]: Timedelta('1 days 02:34:57')
In [4]: round(td)
TypeError: __round__() takes exactly 2 positional arguments (1 given)

Copy link
Member Author

Choose a reason for hiding this comment

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

Or were you replying to my last sentence when I said that I expect round(object) to work without any args

Copy link
Member

Choose a reason for hiding this comment

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

Or were you replying to my last sentence when I said that I expect round(object) to work without any args

That

Copy link
Member

Choose a reason for hiding this comment

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

If this just happens to work due to an implementation detail in CPython, then it seems to me to be an abuse of the Python API and could break in the future. Is supporting other arguments in Python's round documented somewhere?

Copy link
Contributor

@asishm asishm Mar 31, 2024

Choose a reason for hiding this comment

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

python round which is just object.__round__(self[, ndigits])

return self.round(freq)


def truediv_object_array(ndarray left, ndarray right):
cdef:
Expand Down
3 changes: 3 additions & 0 deletions pandas/_libs/tslibs/timestamps.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -2193,6 +2193,9 @@ timedelta}, default 'raise'
"""
return self._round(freq, RoundTo.PLUS_INFTY, ambiguous, nonexistent)

def __round__(self, freq):
return self.round(freq)

@property
def tz(self):
"""
Expand Down