python - 具有本地化日期时间索引的数据框 : how to drop days not having a given time

标签 python python-3.x pandas dataframe

我有一个如下所示的数据框:

                            A   B
2014-06-02 09:00:00-04:00   ... ...
2014-06-02 10:00:00-04:00   ... ...
2014-06-02 11:00:00-04:00   ... ...
2014-06-02 12:00:00-04:00   ... ...
2014-06-03 09:00:00-04:00   ... ...
2014-06-03 10:00:00-04:00   ... ...
2014-06-03 11:00:00-04:00   ... ...
2014-06-04 09:00:00-04:00   ... ...
2014-06-04 10:00:00-04:00   ... ...
2014-06-04 11:00:00-04:00   ... ...
2014-06-04 12:00:00-04:00   ... ...

我需要删除没有时间 12:00:00-04:00 的日子:在我的示例中,它将是 2014-06-03。所以最终的数据框看起来像:

                            A   B
2014-06-02 09:00:00-04:00   ... ...
2014-06-02 10:00:00-04:00   ... ...
2014-06-02 11:00:00-04:00   ... ...
2014-06-02 12:00:00-04:00   ... ...
2014-06-04 09:00:00-04:00   ... ...
2014-06-04 10:00:00-04:00   ... ...
2014-06-04 11:00:00-04:00   ... ...
2014-06-04 12:00:00-04:00   ... ...

请注意,索引已本地化 (-04:00)

Pandas 0.24.2

最佳答案

您可以分组过滤

df.groupby(df.index.date).filter(lambda s: 12 in s.index.hour)

                            A   B
2014-06-02 09:00:00-04:00   ... ...
2014-06-02 10:00:00-04:00   ... ...
2014-06-02 11:00:00-04:00   ... ...
2014-06-02 12:00:00-04:00   ... ...
2014-06-04 09:00:00-04:00   ... ...
2014-06-04 10:00:00-04:00   ... ...
2014-06-04 11:00:00-04:00   ... ...
2014-06-04 12:00:00-04:00   ... ...

关于python - 具有本地化日期时间索引的数据框 : how to drop days not having a given time,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58918666/

相关文章:

python-3.x - python - "Boolean Series key will be reindexed to match DataFrame index. from ipykernel import kernelapp as app"

django - python-social-auth 与 Django : ImportError: No module named 'social_django'

python - 如何在 Python 中使用 selenium 登录网站?

python - 我如何获得 Pandas 的年龄和日期

Pandas read_html 检索表

python - 接近 0.05 的舍入从结果中删除一位

Python AmazonAPI 错误 503 服务不可用

python - 根据列值更新值的简洁方法

python - 无和空字符串的 CSV 阅读器行为

python - 如何按轴选择pandas表的行索引或列索引