Skip to content
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

Loss of datetime precision with Pandas < 0.20 #16

Closed
amacd31 opened this issue Aug 8, 2017 · 0 comments
Closed

Loss of datetime precision with Pandas < 0.20 #16

amacd31 opened this issue Aug 8, 2017 · 0 comments
Labels

Comments

@amacd31
Copy link
Owner

amacd31 commented Aug 8, 2017

With Pandas < 0.20 the precision of datetimes can be lost when writing irregular series.

This can occur when the data to be written can be stored as float32 and Pandas treats the data as float32 instead of float64. The below snippet of code demonstrates the problem and the final assert will fail with older versions of Pandas.


sample = pd.DataFrame(
    pd.np.array(
        [x + 0.1 for x in range(10)],
        dtype=pd.np.float32
    ),
    index=pd.date_range(
        '2017-08-06 06:50:00+00:00',
        periods=10,
        freq='1T'
    )
)

sample['datestamp'] = pd.Series(
    sample.index.map(
        lambda dateval: dateval.value // 1000000000
    ),
    index=sample.index
)

sample.values.astype(pd.np.int64)

print(sample.values.astype(pd.np.int64)[0][1])
assert sample.values.astype(pd.np.int64)[0][1] == 1502002200

This issue stems from the fact that calling .values on a pandas.DataFrame should cast the data types to the a capable common data type. However the bug in pandas results in the int64 being cast to a float32, losing precision in the process, instead of correctly upcasting everything to float64 (which can hold both types successfully).

This bug can be worked around by either ensuring that the time series to be written has a dtype of float64 or you are using Pandas >= 0.20.

@amacd31 amacd31 added the bug label Aug 8, 2017
@amacd31 amacd31 closed this as completed in 719246b Aug 8, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant