-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
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
Conversation
pre-commit.ci autofix |
for more information, see https://pre-commit.ci
@@ -2290,6 +2290,9 @@ class Timedelta(_Timedelta): | |||
div = other // self | |||
return div, other - div * self | |||
|
|||
def __round__(self, freq): |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
)
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think __round__
should be implemented for these objects. Since round
is only well specified for numeric values, I think it's too opaque to apply for datelike objects.
Makes sense, I apologize, I was too hasty to make a PR. |
I would recommend closing this PR until discussion in the issue has resolved |
@mroeschke @MarcoGorelli @rhshadrach Hello, I was the one that created the issue. There is already an implementation of a round method for timedelta pd.Timedelta.round('s') works. (https://pandas.pydata.org/docs/reference/api/pandas.Timedelta.round.html) (I'm going on again because I still use the fix I made here #58071 on my production server and I would be happy to not have to handle this manually but to see the library handle it). |
As commented above (#58081 (comment)):
Do you disagree that making Alternatively, if you are wanting the signature to be something like |
So because round(td, n=5) isn't really clear, you don't want to implement round(td). I disagree that it's a good enough reason to not do it. Have a nice day boys and thanks for this library (I use it every day at work and I love it !<3) |
doc/source/whatsnew/vX.X.X.rst
file if fixing a bug or adding a new feature.