我有一个带有DateTimeIndex的pandas DataFrame:
A B
2016-04-25 18:50:06 440.967796 201.049600
2016-04-25 18:50:13 441.054995 200.767034
2016-04-25 18:50:20 441.142337 200.484475
...
2016-07-27 18:50:06 440.967796 201.049600
2016-07-27 18:50:13 441.054995 200.767034
2016-07-27 18:50:20 441.142337 200.484475
我想使用日期列表提取给定日期yyyy-mm-dd的所有数据:[‘2016-04-25′,’2016-04-28’,…]
我尝试了以下方法:
df[df.index.isin(['2016-04-25', '2016-04-26'])]
Empty DataFrame
我想检索此列表中给出的日期的所有数据(一整天的数据)
解决方法:
您需要先删除时间this solutions:
df = df[df.index.normalize().isin(['2016-04-25', '2016-04-26'])]
df = df[df.index.floor('D').isin(['2016-04-25', '2016-04-26'])]
另一种解决方案是比较DatetimeIndex.date
,但必要时使用numpy.in1d
而不是:
df = df[np.in1d(df.index.date, pd.to_datetime(['2016-04-25', '2016-04-26']).date)]
或者比较创建的字符串DatetimeIndex.strftime
:
df = df[np.in1d(df.index.strftime('%Y-%m-%d'), ['2016-04-25', '2016-04-26'])]
print (df)
A B
2016-04-25 18:50:06 440.967796 201.049600
2016-04-25 18:50:13 441.054995 200.767034
2016-04-25 18:50:20 441.142337 200.484475
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。