从熊猫HDF5Store检索日期范围

时间:2018-08-10 01:32:09

标签: python pandas hdf5 hdfstore

我希望使用Pandas从我的HDFStore中选择一个日期范围。我注意到我的日期时间(毫秒频率)存储为Unix时间,并带有额外的右填充零,即1514797200002000000。我怀疑我的where子句不正确。

这是我的可复制代码:

import pandas as pd
import numpy as np

path_file = "hdf5_store.h5"
data = np.random.randn(5)
cols = ["values"]
date_range = pd.date_range('20180101 09:00:00', periods=5, freq='ms')
df = pd.DataFrame(data=data, index=date_range, columns=cols)

with pd.HDFStore(path_file, mode="w", driver="H5FD_CORE") as store:
        store.append("df", df, format="table", data_columns=["date_time"], index=False)
        store.create_table_index("df", columns=["date_time"], kind="full")
        print(store["df"])

df1 = pd.read_hdf(path_file, "df", where=["date_time>1514797200002000000"])
print(df1)

1 个答案:

答案 0 :(得分:0)

这可以作为查询:

dt = pd.to_datetime(1514797200003000000)
df1 = pd.read_hdf(path_file, "df", where=["index>dt"])