Skip to content

BUG: TypeError("type Timedelta doesn't define __round__ method") #58071

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
3 tasks done
JustAnotherVeryNormalDeveloper opened this issue Mar 29, 2024 · 7 comments
Closed
3 tasks done
Labels
Enhancement Needs Discussion Requires discussion from core team before further action Timedelta Timedelta data type

Comments

@JustAnotherVeryNormalDeveloper
Copy link

JustAnotherVeryNormalDeveloper commented Mar 29, 2024

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd

class CustomTimedelta(pd.Timedelta):  
    def __round__(self, n=None):  
        seconds = self.total_seconds()
        rounded_seconds = round(seconds, n) if n else round(seconds)
        return pd.to_timedelta(rounded_seconds, unit='s') 

# Uncomment this to solve the problem
# pd.Timedelta = CustomTimedelta

# Example usage  
td = pd.Timedelta(days=1, hours=2, minutes=34, seconds=56.78)
print(round(td))  # prints '1 days 02:35:00'

Issue Description

I try to round the value, actually I want to round for the seconds. I was surprised to see that there are no default round method ?
Best use of round would be to precise the type of round (day/seconds/...) but I think it should have a default behavior.
In a pythonic way.
And I think seconds should be the default.

Expected Behavior

It should have a default behavior, in a pythonic way. I propose seconds for the default.

Installed Versions

INSTALLED VERSIONS

commit : fd3f571
python : 3.9.19.final.0
python-bits : 64
OS : Linux
OS-release : 4.4.0-19041-Microsoft
Version : #3996-Microsoft Thu Jan 18 16:36:00 PST 2024
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : fr_FR.UTF-8
LOCALE : fr_FR.UTF-8

pandas : 2.2.0
numpy : 1.26.3
pytz : 2024.1
dateutil : 2.9.0.post0
setuptools : 69.2.0
pip : 24.0
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 5.1.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.3
IPython : 8.18.1
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.3
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.2
numba : None
numexpr : None
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : None
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.12.0
sqlalchemy : 2.0.28
tables : None
tabulate : None
xarray : None
xlrd : None
zstandard : None
tzdata : 2024.1
qtpy : None
pyqt5 : None

@JustAnotherVeryNormalDeveloper JustAnotherVeryNormalDeveloper added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Mar 29, 2024
@JustAnotherVeryNormalDeveloper JustAnotherVeryNormalDeveloper changed the title BUG: default method BUG: TypeError("type Timedelta doesn't define __round__ method") Mar 29, 2024
@Aloqeely
Copy link
Member

Does the documentation say that Timedelta can be rounded? If not, I think this issue should be labeled as a feature request rather than a bug report, with a clear title that describes the desired feature.

@JustAnotherVeryNormalDeveloper
Copy link
Author

yes it can @Aloqeely :)
(https://pandas.pydata.org/docs/reference/api/pandas.Timedelta.round.html)
The thing is that it doesn't have a default value yet, you have to precise the parameter freq.
So you are right to say it's more about a feature rater than a bug.

Yet, I find it weird to crash on a simple round() when the library think about doing serie.round() !

@rhshadrach
Copy link
Member

When reading code, I do not think it is clear what round(td, n=5) will do, as opposed to td.round(freq="5s"). As such, I do not think we should support Python's round.

@rhshadrach rhshadrach added Enhancement Needs Discussion Requires discussion from core team before further action Closing Candidate May be closeable, needs more eyeballs Timedelta Timedelta data type and removed Bug Needs Triage Issue that has not been reviewed by a pandas team member Closing Candidate May be closeable, needs more eyeballs labels Mar 31, 2024
@Aloqeely
Copy link
Member

I believe the suggestion was to have a default behavior for Python's round on timedeltas such that round(td) works without any extra args.
And seconds being the default freq makes some sense as seconds are the fractional part of the timedelta
(I.E. td = 15 days 07:22:56.782037999 --> round(td) = 15 days 07:22:57)

@JustAnotherVeryNormalDeveloper
Copy link
Author

Perfect reformulation @Aloqeely :)
The question is still "should we do it ?" or "how to do it ?" ?

For the "should we do it ?" I would say seconds is a logical default choice. And we like default behavior in python even if we all prefer specific precision, it's always appreciable to make code work in a really short way.

For the 'how to do it ?" I would just Override the round method as I did on my custom class (and so I won't have to do it in my project and the future projects but the library would handle it). But it's maybe too simple and I miss side effects ?

Should I propose a pull request ?

@jbrockmendel
Copy link
Member

Based on discussion in #58081 can we close this as no-action?

@mroeschke
Copy link
Member

Yeah thanks for the suggestion but closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Needs Discussion Requires discussion from core team before further action Timedelta Timedelta data type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants