Skip to content

BUG: datetime column changed after storing to hdf5 #57085

Closed
@xbanke

Description

@xbanke

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugIO HDF5read_hdf, HDFStoreNon-Nanodatetime64/timedelta64 with non-nanosecond resolution

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions