Skip to content

BUG: Timedelta resolution is different depending on how the argument is passed #33992

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

Open
2 of 3 tasks
xlambein opened this issue May 5, 2020 · 3 comments
Open
2 of 3 tasks
Assignees
Labels
Bug Timedelta Timedelta data type

Comments

@xlambein
Copy link

xlambein commented May 5, 2020

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

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

  • (optional) I have confirmed this bug exists on the master branch of pandas.


Code Sample, a copy-pastable example

>>> import pandas as pd
>>> pd.Timedelta(1 / 128, "seconds").resolution_string
'N'
>>> pd.Timedelta(seconds=1 / 128).resolution_string
'U'

Problem description

The resolution of Timedelta differs when created with value and unit, versus with keyword arguments. When tested with seconds as units, the former will round to nanoseconds, while the latter rounds to microseconds.

Expected Output

They should both have the same resolution, nanoseconds making the most sense to me:

>>> pd.Timedelta(seconds=1 / 128).resolution_string
'N'

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.2.final.0
python-bits : 64
OS : Darwin
OS-release : 19.4.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : None
LOCALE : None.UTF-8

pandas : 1.0.3
numpy : 1.18.4
pytz : 2020.1
dateutil : 2.8.1
pip : 20.0.2
setuptools : 46.1.3.post20200330
Cython : None
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : None
matplotlib : None
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : None
sqlalchemy : None
tables : None
tabulate : None
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@xlambein xlambein added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 5, 2020
@xcz011
Copy link
Contributor

xcz011 commented May 11, 2020

I think in here https://github.com/pandas-dev/pandas/blob/master/pandas/_libs/tslibs/timedeltas.pyx#L1201
timedelta(**kwargs) trim the nano second by round nano second. Baiclly,
https://github.com/python/cpython/blob/master/Lib/datetime.py#L551 is not support nano second cause this.
I feel we could add function to save the nano second before we pass to timedelta function or we could match all the **kwargs to value and unit

@xcz011
Copy link
Contributor

xcz011 commented May 11, 2020

take

@jbrockmendel jbrockmendel added Timedelta Timedelta data type and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Jun 5, 2020
@patrickmckenna
Copy link

I think in here https://github.com/pandas-dev/pandas/blob/master/pandas/_libs/tslibs/timedeltas.pyx#L1201
timedelta(**kwargs) trim the nano second by round nano second. Baiclly,
https://github.com/python/cpython/blob/master/Lib/datetime.py#L551 is not support nano second cause this.
I feel we could add function to save the nano second before we pass to timedelta function or we could match all the **kwargs to value and unit

It looks like #45108 handled this, no?

FWIW, #46936 adds tests to verify equality between instances created with equivalent kwargs, unit, and str values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Timedelta Timedelta data type
Projects
None yet
Development

No branches or pull requests

4 participants