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

BUG: datetime column changed after storing to hdf5 #57085

Closed
3 tasks done
xbanke opened this issue Jan 26, 2024 · 5 comments
Closed
3 tasks done

BUG: datetime column changed after storing to hdf5 #57085

xbanke opened this issue Jan 26, 2024 · 5 comments
Labels
Bug IO HDF5 read_hdf, HDFStore Non-Nano datetime64/timedelta64 with non-nanosecond resolution

Comments

@xbanke
Copy link

xbanke commented Jan 26, 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 numpy as np
import pandas as pd


np.random.seed(42)
df = pd.DataFrame(np.random.randn(5, 3), columns=list('abc')).assign(dt=pd.Timestamp('2024-01-01'))  # .astype({'dt': 'datetime64[ns]'})
print(f'{"Before storing to HDF5":-^80}')
print(df.dtypes)
print(df)
df.to_hdf('df.h5', key='df')
print(f'{"After storing to HDF5":-^80}')
df1 = pd.read_hdf('df.h5')
print(df1.dtypes)
print(df1)


# -----------------------------Before storing to HDF5-----------------------------
# a           float64
# b           float64
# c           float64
# dt    datetime64[s]
# dtype: object
#           a         b         c         dt
# 0  0.496714 -0.138264  0.647689 2024-01-01
# 1  1.523030 -0.234153 -0.234137 2024-01-01
# 2  1.579213  0.767435 -0.469474 2024-01-01
# 3  0.542560 -0.463418 -0.465730 2024-01-01
# 4  0.241962 -1.913280 -1.724918 2024-01-01
# -----------------------------After storing to HDF5------------------------------
# a            float64
# b            float64
# c            float64
# dt    datetime64[ns]
# dtype: object
#           a         b         c                            dt
# 0  0.496714 -0.138264  0.647689 1970-01-01 00:00:01.704067200
# 1  1.523030 -0.234153 -0.234137 1970-01-01 00:00:01.704067200
# 2  1.579213  0.767435 -0.469474 1970-01-01 00:00:01.704067200
# 3  0.542560 -0.463418 -0.465730 1970-01-01 00:00:01.704067200
# 4  0.241962 -1.913280 -1.724918 1970-01-01 00:00:01.704067200

Issue Description

Creating a dataframe with a datetime column by assigning a single pd.Timestamp, then storing it with to_hdf. the datetime column changed after reading from the hdf5 file.
But, if astype manually before storing, everthing will be OK.

Expected Behavior

df should equal to df1

Installed Versions

INSTALLED VERSIONS

commit : fd3f571
python : 3.11.4.final.0
python-bits : 64
OS : Linux
OS-release : 5.15.0-78-generic
Version : #85-Ubuntu SMP Fri Jul 7 15:25:09 UTC 2023
machine : x86_64
processor : x86_64
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 2.2.0
numpy : 1.24.3
pytz : 2023.3
dateutil : 2.8.2
setuptools : 66.0.0
pip : 23.0.1
Cython : 3.0.0b2
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : 3.1.0
lxml.etree : 4.9.2
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 3.1.2
IPython : 8.16.1
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : 4.12.2
bottleneck : None
dataframe-api-compat : None
fastparquet : None
fsspec : 2023.9.0
gcsfs : None
matplotlib : 3.7.1
numba : 0.57.0
numexpr : 2.8.4
odfpy : None
openpyxl : 3.1.2
pandas_gbq : None
pyarrow : 12.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.10.1
sqlalchemy : 2.0.21
tables : 3.8.0
tabulate : None
xarray : None
xlrd : 2.0.1
zstandard : None
tzdata : 2023.3
qtpy : None
pyqt5 : None

@xbanke xbanke added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Jan 26, 2024
@PritamSarbajna
Copy link

Hey @xbanke, I've tried it in Jupyter and Colab but it is working correctly.

Colab :

Screenshot (302)

Jupyter :

Screenshot (303)

@xbanke
Copy link
Author

xbanke commented Jan 29, 2024

@PritamSarbajna, So what is the possible problem of my environment?

@xbanke
Copy link
Author

xbanke commented Jan 30, 2024

There exists some difference:

import numpy as np
import pandas as pd

np.random.seed(42)

df = pd.DataFrame(np.random.randn(5, 3), columns=list('abc')).assign(dt=pd.Timestamp('20240101'))
df1 = df.astype({'dt': 'datetime64[ns]'})

print(df['dt'].values)
print(df1['dt'].values)


# array(['2024-01-01T00:00:00', '2024-01-01T00:00:00',
#        '2024-01-01T00:00:00', '2024-01-01T00:00:00',
#        '2024-01-01T00:00:00'], dtype='datetime64[s]')
# array(['2024-01-01T00:00:00.000000000', '2024-01-01T00:00:00.000000000',
#        '2024-01-01T00:00:00.000000000', '2024-01-01T00:00:00.000000000',
#        '2024-01-01T00:00:00.000000000'], dtype='datetime64[ns]')

@rhshadrach
Copy link
Member

Thanks for the report; given that non-nano resolutions are relatively new, I'm guessing that it's not yet built into hdf. Further investigations welcome!

cc @jbrockmendel

@rhshadrach rhshadrach added IO HDF5 read_hdf, HDFStore Non-Nano datetime64/timedelta64 with non-nanosecond resolution and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 11, 2024
@mroeschke
Copy link
Member

Closed by #59018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug IO HDF5 read_hdf, HDFStore Non-Nano datetime64/timedelta64 with non-nanosecond resolution
Projects
None yet
Development

No branches or pull requests

4 participants