在pandas中,如何使用“where”参数查询datetime索引列?

2024-04-25 14:41:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我使用append_to_multiple将dataframe附加到两个表中,选择器是一个datetime列。就这样。你知道吗

store.append_to_multiple({'time': ['time'], 'data': ['field1', 'field2']}, 
    df, selector='time')

然后,现在我想用select_as_multiple和where参数来查询time字段。
我想这样做,但是查询datetime字段。你知道吗

store.select_as_multiple(['df1_mt', 'df2_mt'], where=['A>0', 'B>0'],
    selector = 'df1_mt')

所以,where参数的元素必须是字符串,那么如何使用datetime索引进行查询呢?你知道吗


Tags: tostoredataframe参数datetimetimeas选择器
1条回答
网友
1楼 · 发布于 2024-04-25 14:41:07

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

相关问题 更多 >

    热门问题