Skip to content

[BUG]: Wrong unix timestamp parsing with floating point using pd.to_datetime #13834

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
lopezco opened this issue Jul 29, 2016 · 3 comments
Closed
Labels
Bug Datetime Datetime data dtype Dtype Conversions Unexpected or buggy dtype conversions

Comments

@lopezco
Copy link

lopezco commented Jul 29, 2016

Hello!

I'm trying to parse floating point timestamps with pd.to_datetime but the only way that it works is using the deprecated parameter coerce=True

For example, without coerce=True the result is wrong.

>>> pd.to_datetime(1.1, unit='s')  # Expected result Timestamp('1970-01-01 00:00:01.100000')
Timestamp('1970-01-01 00:00:01')

However, with coerce=True the result is correct.

>>> pd.to_datetime(1.1, unit='s', coerce=True)  # Expected result Timestamp('1970-01-01 00:00:01.100000')
Timestamp('1970-01-01 00:00:01.100000')

I'm happy to help if you need more details.

Code Sample (copy-pastable example)

import pandas as pd
expected_result = pd.Timestamp('1970-01-01 00:00:01.100000')
timestamp1 = pd.to_datetime(1.1, unit='s')
timestamp2 = pd.to_datetime(1.1, unit='s', coerce=True)

print("Result without coerce: {}".format(timestamp1 == expected_result))
print("Result with coerce: {}".format(timestamp2 == expected_result))

pd.show_versions() output

INSTALLED VERSIONS
------------------
commit: None
python: 3.4.3.final.0
python-bits: 64
OS: Linux
OS-release: 3.19.0-65-generic
machine: x86_64
processor: x86_64
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.18.1
nose: 1.3.7
pip: 8.1.2
setuptools: 19.2
Cython: 0.24.1
numpy: 1.11.1
scipy: 0.17.0
statsmodels: 0.6.1
xarray: None
IPython: 4.2.0
sphinx: 1.4.1
patsy: 0.4.1
dateutil: 2.5.3
pytz: 2016.6.1
blosc: None
bottleneck: 1.1.0
tables: None
numexpr: 2.5.2
matplotlib: 1.5.1
openpyxl: None
xlrd: 0.9.4
xlwt: 1.0.0
xlsxwriter: None
lxml: None
bs4: 4.4.1
html5lib: 0.999
httplib2: 0.8
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: 2.6.1 (dt dec pq3 ext lo64)
jinja2: 2.8
boto: None
pandas_datareader: None
@chris-b1
Copy link
Contributor

Problem is around here - there's some kind of fastpath that tries directly converting to integers, but doesn't seem to handle truncation correctly.
https://github.com/pydata/pandas/blob/master/pandas/tslib.pyx#L2098

Note that you can also workaround this using the non-deprecated errors kwarg.

In [41]: pd.to_datetime(1.1, unit='s', errors='coerce')
Out[41]: Timestamp('1970-01-01 00:00:01.100000')

@lopezco
Copy link
Author

lopezco commented Jul 29, 2016

Thank you the workaround!

@jreback
Copy link
Contributor

jreback commented Jul 29, 2016

@chris-b1

This should cast like this

 In [14]: np.array([1.1]).astype('i8', casting='no')
TypeError: Cannot cast array from dtype('float64') to dtype('int64') according to the rule 'no'

instead I think (so this will then hit the other path)

@jreback jreback added Bug Datetime Datetime data dtype Dtype Conversions Unexpected or buggy dtype conversions labels Jul 29, 2016
@jreback jreback added this to the Next Major Release milestone Jul 29, 2016
@jreback jreback closed this as completed in 49f99ac Aug 9, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Datetime Datetime data dtype Dtype Conversions Unexpected or buggy dtype conversions
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants