Pandas HD5 查询,表达式失败

Pandas HD5-query, where expression fails

提问人:user3276418 提问时间:11/27/2014 最后编辑:filmoruser3276418 更新时间:1/3/2023 访问量:1416

问:

我想查询一个 HDF5 文件。我愿意

df.to_hdf(pfad,'df', format='table')

将 DataFrame 写入磁盘。

阅读我使用

hdf = pandas.HDFStore(pfad)

我有一个列表,其中包含称为过期的值,并尝试将 hd5 表的部分读取到数据帧中,该数据帧的值介于列之间和列中。列过期条目的格式为 。numpy.datetime64expirations[1]expirations[0]"expiration"Timestamp('2002-05-18 00:00:00')

我使用以下命令:

df = hdf.select('df',
                where=['expiration<expiration[1]','expiration>=expirations[0]'])

但是,此操作将失败并产生值错误:

ValueError: The passed where expression: [expiration=expirations[0]]
            contains an invalid variable reference
            all of the variable refrences must be a reference to
            an axis (e.g. 'index' or 'columns'), or a data_column
            The currently defined references are: index,columns
python-2.7 熊猫

评论

5赞 Jeff 11/27/2014
保存帧时需要将查询列指定为data_column;请参阅文档 pandas.pydata.org/pandas-docs/stable/...
0赞 user3276418 11/27/2014
我使用了 df.to_hdf(pfad,'df', format='table')。
1赞 Jeff 11/28/2014
您需要指定data_columns如文档中所示
0赞 user3276418 11/28/2014
当我使用 store 对象编写 hd5 文件时,我遇到了这个问题: stackoverflow.com/questions/27174705/querying-a-hdf-store

答:

0赞 Lorenzo Bassetti 1/3/2023 #1

你能试试这个代码吗?

df = hdf.select('df', where='expiration < expirations[1] and expiration >= expirations[0]')

或者,作为查询:

df = hdf.query('expiration < @expirations[1] and expiration >= @expirations[0]')

不确定哪一个最适合您的情况,我注意到您正在尝试使用“where”来过滤行,没有字符串或列表,这有意义吗?