我使用append_to_multiple
将数据帧附加到2个表中,选择器是一个日期时间列。像这样。
store.append_to_multiple({'time': ['time'], 'data': ['field1', 'field2']},
df, selector='time')
然后,现在我想使用 select_as_multiple
和 where 参数来查询 time
字段。
我想这样做,但查询日期时间字段。
store.select_as_multiple(['df1_mt', 'df2_mt'], where=['A>0', 'B>0'],
selector = 'df1_mt')
那么,where
参数的元素一定是字符串,那么如何使用日期时间索引进行查询呢?
最佳答案
如 docs 中所述,您可以使用 YYYYMMDD int/string 或内联 Timestamp
表达式。
In [20]: df = pd.DataFrame({'a':pd.date_range('2014-1-1', periods=300)})
In [21]: df.to_hdf('store.h5', 'df', format='table', data_columns=True)
In [25]: pd.read_hdf('store.h5', 'df', where='a > 20141015')
Out[25]:
a
288 2014-10-16
289 2014-10-17
290 2014-10-18
291 2014-10-19
292 2014-10-20
293 2014-10-21
294 2014-10-22
295 2014-10-23
296 2014-10-24
297 2014-10-25
298 2014-10-26
299 2014-10-27
In [26]: pd.read_hdf('store.h5', 'df', where='a > Timestamp("10/15/2014")')
Out[26]:
a
288 2014-10-16
289 2014-10-17
290 2014-10-18
291 2014-10-19
292 2014-10-20
293 2014-10-21
294 2014-10-22
295 2014-10-23
296 2014-10-24
297 2014-10-25
298 2014-10-26
299 2014-10-27
关于python - 在 pandas 中,如何使用 "where"参数来查询日期时间索引列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33358283/