[英]How to filter a pandas DatetimeIndex by day of week and hour in the day
I have a pandas DatetimeIndex and I would like to filter the index by the criterion that the day of the week and hour of the day matches a list.我有一个 Pandas DatetimeIndex,我想根据一周中的哪一天和一天中的小时匹配列表的标准来过滤索引。 For example, I have of list of tuples indicating valid (day of week, hour, minute) for each TimeStamp:例如,我有一个元组列表,指示每个时间戳的有效(星期几、小时、分钟):
[(4, 6), (5, 7)]
The final index should only contain date times that are Friday(day_of_week = 4) hour 6 or Saturday(day_of_week = 5) hour 7.最终索引应仅包含星期五(day_of_week = 4) 小时 6 或星期六(day_of_week = 5) 小时 7 的日期时间。
Lets say the input data frame is like:假设输入数据框是这样的:
2016-04-02 06:30:00 1
2016-04-02 06:45:00 2
2016-04-02 07:00:00 3
2016-04-02 07:15:00 4
2016-04-03 07:30:00 5
2016-04-03 07:45:00 6
2016-04-03 08:00:00 7
After the filter, it should be like:过滤后,它应该是这样的:
2016-04-02 06:30:00 1
2016-04-02 06:45:00 2
2016-04-03 07:30:00 5
Because I only keep indices whose day of week and hour of the day in the list [(4, 6), (5, 7)]
因为我只保留列表[(4, 6), (5, 7)]
You should add a column day_of_week
and a column hour
, then you can filer on this columns.您应该添加一列day_of_week
和一列hour
,然后您可以在此列上进行筛选。
For example :例如:
df["day_of_week"] = df["date"].dayofweek()
df["hour"] = df["date"].hour()
pd.concat([
df.loc[df["day_of_week"].isin(x[0]) & df["hour"].isin(x[1])]
for x in [(4, 6), (5, 7)]
])
Note that I iterate over all your conditions, then I concatenate all the resulting dataframe.请注意,我遍历您的所有条件,然后连接所有结果数据帧。
You could store the dayofweek
and hour
methods from your index
in variables, and then use them with iloc
to filter:你可以存储在dayofweek
和hour
从你的方法index
变量,然后用它们iloc
进行筛选:
dayofweek = df.index.dayofweek
hour = df.index.hour
df.iloc[((dayofweek == 4) & (hour == 6)) | ((dayofweek == 5) & (hour == 7))]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.