python - 如何使用包含过滤数据帧 DatetimeIndex

标签 python pandas

我有一个按日期时间索引的数据框。我正在尝试创建某种过滤器,它只提供包含特定时间的帧。
例如,所有包含“09:30”的帧

df.dtypes
open             float64
high             float64
low              float64
close            float64
volume             int64
returns          float64
returns_final      int64
dtype: object
我可以根据帧的开头执行此操作,但我不知道我们是否可以使用通配符类型过滤器。

df.loc['2020'].head(5)

                    open        high         low        close         volume    returns returns_final
Datetime                            
2020-07-06 09:30:00 255.337982  261.950012  253.208786  261.421997  6592145 -6.084015   1
2020-07-06 11:00:00 261.526001  268.399994  261.239990  266.275452  4955678 -4.749451   1
2020-07-06 12:30:00 266.269043  266.989990  264.200012  265.191986  2002640 1.077057    -1
2020-07-06 14:00:00 265.185455  269.558014  261.597992  268.513763  3303263 -3.328308   1
2020-07-06 15:30:00 268.528015  275.558014  268.096008  274.200012  2583149 -5.671997   1
2020-07-07 09:30:00 281.002014  285.641998  267.341980  277.621979  10130111    3.380035    -1
2020-07-07 11:00:00 278.000000  284.600006  276.536011  278.123718  4221461 -0.123718   1
2020-07-07 12:30:00 278.000000  282.000000  277.399994  280.131012  2394459 -2.131012   1
我正在尝试沿着这些方向找到一些东西
df.loc['*09:30*']
想我可能不得不重新索引这个或其他东西
任何输入将不胜感激

最佳答案

你可以使用这个:

df[df.index.strftime('%H:%M:%S') == '09:30:00']
输出:
                           open        high         low       close    volume   returns  return_final
Datetime                                                                                             
2020-07-06 09:30:00  255.337982  261.950012  253.208786  261.421997   6592145 -6.084015             1
2020-07-07 09:30:00  281.002014  285.641998  267.341980  277.621979  10130111  3.380035            -1
对于一天中的多次:
df[df.index.strftime('%H:%M:%S').isin(['09:30:00','11:00:00'])]
您可以使用过滤器,就像使用正则表达式一样:
df.filter(regex='09:30|11:00', axis=0)
输出:
                                      open        high         low              close    volume  returns  return_final
Datetime                                                                                                              
2020-07-06 09:30:00 255.337982  261.950012  253.208786  261.421997  6592145 -6.084015  1.000000      NaN           NaN
2020-07-06 11:00:00 261.526001  268.399994  261.239990  266.275452  4955678 -4.749451  1.000000      NaN           NaN
2020-07-07 09:30:00 281.002014  285.641998  267.341980  277.621979           10130111  3.380035     -1.0           NaN
2020-07-07 11:00:00 278.000000  284.600006  276.536011  278.123718  4221461 -0.123718  1.000000      NaN           NaN

关于python - 如何使用包含过滤数据帧 DatetimeIndex,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64079463/

相关文章:

python - 如何阻止 python pandas 将 "00:00:00"添加到每个日期?

python - 使用计数和百分比创建列联表 Pandas

python - tkinter 中的 for 循环问题

python - 如何在python的循环中迭代mongo游标

python - SublimeCodeIntel 自动完成在 Pandas 和 Numpy 上失败

python - pandas groupby 转置 str 列

python - 斐波那契数列和返回问题?

python - 如何使用 python 从 xlsx 文件中的一列中减去另一列中的单元格值

pandas - 如何在多个 DataFrame 中删除行?

python-2.7 - 在 pandas.DataFrame.to_csv 中写入多个标题行