[英]select dataframe rows (datetimeindex) by a list of datetime.date
df看起來像這樣:
DateTime
2017-07-10 03:00:00 288.0
2017-07-10 04:00:00 306.0
2017-08-10 05:00:00 393.0
2017-08-10 06:00:00 522.0
2017-09-10 07:00:00 487.0
2017-09-10 08:00:00 523.0
2017-10-10 09:00:00 585.0
質疑如何在日期列表中選擇該行:
['2017-07-10', '2017-09-10']
擁有:
DateTime
2017-07-10 03:00:00 288.0
2017-07-10 04:00:00 306.0
2017-09-10 07:00:00 487.0
2017-09-10 08:00:00 523.0
謝謝
假設Datetime是索引,請嘗試以下操作:
to_search=['2017-07-10', '2017-09-10']
df[df.index.to_series().dt.date.astype(str).isin(to_search)]
1
DateTime
2017-07-10 03:00:00 288.0
2017-07-10 04:00:00 306.0
2017-09-10 07:00:00 487.0
2017-09-10 08:00:00 523.0
鑒於在列表中的日期,包含了日常的信息,您可以通過地板(啟動Series.dt.floor
)的DatetimeIndex
到日常水平和索引使用datetime對象的名單isin
:
t = [pd.to_datetime('2017-07-10'), pd.to_datetime('2017-09-10')]
df.index= pd.to_datetime(df.index)
df[df.index.floor('d').isin(t)]
輸出量
DateTime
2017-07-10 03:00:00 288.0
2017-07-10 04:00:00 306.0
2017-09-10 07:00:00 487.0
2017-09-10 08:00:00 523.0
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.