python - 在 pandas 中,如何使用 "where"参数来查询日期时间索引列?

标签 python pandas

我使用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/

相关文章:

python - 惯用的 Python : 'times' loop

Python 按字长计算字数

python - 如何替换pandas中的数据框

python - 如何将功能应用于适当的数据框

python - Pandas Dataframe 检索唯一列

python - Pandas 有没有办法从系列中删除重复项?

python - 当Python中的值为 "00/00/0000"时,如何省略CSV文件中的单元格

python - 学习Pygame,为什么这个 'raindrop'(矩形对象)不会掉落?

javascript - 一些Python问题

Python 异常传播