Skip to content

to_timedelta does not work properly with nanoseconds #19680

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
Sup3rGeo opened this issue Feb 13, 2018 · 2 comments
Closed

to_timedelta does not work properly with nanoseconds #19680

Sup3rGeo opened this issue Feb 13, 2018 · 2 comments
Labels
Bug Duplicate Report Duplicate issue or pull request Timedelta Timedelta data type

Comments

@Sup3rGeo
Copy link
Contributor

Code Sample, a copy-pastable example if possible

>>> index = pandas.to_timedelta([15e-7], unit="s")
>>> index[0].nanoseconds
0
>>> index[0].microseconds
2

Problem description

nanoseconds range input for to_timedelta function are rounded to microseconds.

Expected Output

>>> index = pandas.to_timedelta([15e-7], unit="s")
>>> index[0].nanoseconds
500
>>> index[0].microseconds
1

However this works fine:

>>> index = pandas.to_timedelta([1.5], unit="us")
>>> index[0].nanoseconds
500
>>> index[0].microseconds
1

So it seems to be something when se choose unit="s"

Output of pd.show_versions()

INSTALLED VERSIONS

commit: None
python: 3.6.3.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 158 Stepping 9, GenuineIntel
byteorder: little
LC_ALL: None
LANG: None
LOCALE: None.None
pandas: 0.21.1
pytest: 3.4.0
pip: 9.0.1
setuptools: 38.4.0
Cython: 0.27.3
numpy: 1.13.3
scipy: 1.0.0
pyarrow: None
xarray: None
IPython: 6.2.1
sphinx: 1.6.6
patsy: None
dateutil: 2.6.1
pytz: 2017.2
blosc: None
bottleneck: None
tables: None
numexpr: None
feather: None
matplotlib: 2.1.1
openpyxl: 1.7.0
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: 1.0.1
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.10
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@jorisvandenbossche jorisvandenbossche added Bug Timedelta Timedelta data type labels Feb 15, 2018
@jorisvandenbossche
Copy link
Member

@Sup3rGeo thanks for the report!

It seems this happens in this function:

In [126]: pd._libs.tslibs.timedeltas.cast_from_unit(1.5e-6, 's')
Out[126]: 2000

because we round at 6 decimals:

frac = round(frac, p)

Not fully sure why we choose 6 decimals to be allowed, and not 9

@jorisvandenbossche
Copy link
Member

Ah, this is a duplicate of #14156. Continuing discussion there further.

@jorisvandenbossche jorisvandenbossche added the Duplicate Report Duplicate issue or pull request label Feb 15, 2018
@jorisvandenbossche jorisvandenbossche added this to the No action milestone Feb 15, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Duplicate Report Duplicate issue or pull request Timedelta Timedelta data type
Projects
None yet
Development

No branches or pull requests

2 participants