【问题标题】:Querying a HDF-store查询 HDF 存储
【发布时间】:2015-01-26 06:56:43
【问题描述】:

我创建了一个 hd5 文件

hdf=pandas.HDFStore(pfad)
hdf.append('df', df, data_columns=True)

我有一个包含 numpy.datetime64 值的列表,称为过期,并尝试将 hd5 表的部分读入数据帧,该数据帧的值在“过期”列中的过期 [1] 和过期 [0] 之间。列过期条目的格式为 Timestamp('2002-05-18 00:00:00')。

我使用以下命令:

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

但是,我得到 ValueError: Unable to parse x 这应该如何正确完成?

df.dtypes
Out[37]: 
adjusted stock close price           float64
expiration                    datetime64[ns]
strike                                 int64
call put                              object
ask                                  float64
bid                                  float64
volume                                 int64
open interest                          int64
unadjusted stock price               float64

df.info
Out[36]: 
<bound method DataFrame.info of             adjusted stock close price expiration  strike call put      ask  date                                                                          
2002-05-16                     5047.00 2002-05-18    4300        C  802.000   

还有更多列,但查询对它们不感兴趣。

【问题讨论】:

  • 必须是字符串而不是元组,并且您的名称拼写错误
  • 也有助于显示 df.info() 和你的 pandas 版本
  • 确实,这个拼写错误也在我的代码 sn-p 中。现在是以下错误: ValueError: cannot process expression [expiration=expirations[0]], [((expiration) = (2002-05-18T02:00:00.000000000+0200))] 不是有效条件 Pandas 版本:0.14.0 df.info: [3233578 rows x 9 columns]
  • 请用df.info() 更新您的问题(例如,需要查看实际的数据类型)。
  • 你这里有一个disconnet,你在查询中显示的和上面的不一样。 expiration 似乎是 datetime64[ns] 列,但您显示的是 tz 感知日期时间。从您正在做的事情开始,展示一个完整的可复制粘贴示例。

标签: pandas hdf


【解决方案1】:

问题解决了!

我通过

获得了过期时间
 df_expirations=df.drop_duplicates(subset='expiration')
 expirations=df['expiration'].values

这显然将数字格式从 datetime 更改为 tz datetime。 我使用

重新定义了这个
 expirations=df['expirations']

现在这个查询正在运行: 德尔DF df=hdf.select('df', where=('expiration=expirations[1]'))

感谢您指出日期时间格式问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    • 2018-03-11
    • 2018-07-31
    • 1970-01-01
    • 2016-02-02
    相关资源
    最近更新 更多