如何用pandas提取指定时间段的数据
程序员文章站
2024-02-03 14:49:10
处理数据时,经常用到的一个需求是:从全部数据中提取某个时间段内的数据order_data['start_time'] = pd.to_datetime(order_data['start_time'], format="%Y/%m/%d %H:%M:%S")如获取1月到4月份的数据order_data[order_data['start_time'].dt.month.isin(np.arange(1, 5))]如获取2020-01-02到2020-01-12的数据open_day='20....
处理数据时,经常用到的一个需求是:从全部数据中提取某个时间段内的数据
order_data['start_time'] = pd.to_datetime(order_data['start_time'], format="%Y/%m/%d %H:%M:%S")
如获取1月到4月份的数据
order_data[order_data['start_time'].dt.month.isin(np.arange(1, 5))]
如获取2020-01-02到2020-01-12的数据
open_day='2020-01-02 00:00'
close_day='2020-01-12 23:59'
con1=order_data['judge_time']>=open_day
con2=order_data['judge_time']<=close_day
order_data=order_data[con1&con2]
如果是获取每一天的某个时间段,是整点的话(如6:00-22:00)也可以获取hour来判断
order_data[order_data['start_time'].dt.hour.isin(np.arange(6, 22))]
但如果是需要6:00-8:30呢,这就有点麻烦了,经过我实验,可以有下面两种:
import pandas as pd
from dateutil import parser
open_time='06:00'
close_time='08:30'
def is_needtime(x):
con1=x.start_time >= parser.parse(x.date+" "+ open_time)
con2=x.start_time <= parser.parse(x.date+" "+ close_time)
if con1&con2:
return 1
else:
return 0
order_data['start_time'] = pd.to_datetime(order_data['start_time'], format="%Y/%m/%d %H:%M:%S")
order_data['date']=order_data['start_time'].apply(lambda x: x.strftime('%Y/%m/%d'))
order_data=order_data[order_data.apply(is_needtime,axis=1)]
但是这种合规的有点慢,每一次都需要用原来的日期加上时间构造来判断
另一种有点歪路子的方法是我们把时间提取出来,然后统一加上某一天,构造出来一列专门用于判断的带日期的时间列(推荐这种)
import pandas as pd
order_data['start_time'] = pd.to_datetime(order_data['start_time'], format="%Y/%m/%d %H:%M:%S")
order_data['time_judge'] =order_data['start_time'].apply(lambda x: "2020-01-01"+" "+x.strftime('%H:%M'))
order_data['time_judge'] = pd.to_datetime(order_data['time_judge'], format="%Y-%m-%d %H:%M:%S")
open_time='2020-01-01 06:00'
close_time='2020-01-01 08:30'
con1=order_data['judge_time']>=open_time
con2=order_data['judge_time']<=close_time
order_data=order_data[con1&con2]
本文地址:https://blog.csdn.net/qq_38412868/article/details/107445068