use*_*867 5 python datetime pandas
我有一个像下面这样的 df
col1, mydate
1, 25-DEC-2016 09:15:00
2, 25-DEC-2016 10:14:00
3, 25-DEC-2016 10:16:00
4, 25-DEC-2016 10:18:56
2, 25-DEC-2016 11:14:00
2, 25-DEC-2016 10:16:00
df.info(): mydate 323809 non-null object
Run Code Online (Sandbox Code Playgroud)
我需要根据时间使用此数据框,例如 df 的时间小于 10:15:00,df 的时间小于 11:15:00
所以创建了我的切片间隔使用
times=[pd.to_datetime(i) for i in '10:15:00','11:15:00','12:15:00','13:15:00','14:15:00','15:15:00', '15:30:00']
Run Code Online (Sandbox Code Playgroud)
然后我将 mydate 类型转换为时间,这需要很多时间
df['mydate']=df4.mydate.apply(lambda x: pd.to_datetime(x,infer_datetime_format=True).time())
Run Code Online (Sandbox Code Playgroud)
我认为可以优化上述命令,或者应该有更好/更快的方法。
然后我简单地做
for time in times:
slice = df[df.mydate<time.time()]
Run Code Online (Sandbox Code Playgroud)
我的目的只是将 df.mydate 时间与['10:15:00','11:15:00','12:15:00','13:15:00','14:15:00','15:15:00', '15:30:00'] (但不是日期)进行比较,并简单地将 df 子集
上述方法对我来说很好,但我正在寻找更好的方法。
附加:有趣的是排序 mydate 非常快(即使我没有将 mydate col 转换为 datetime)使用
df.sort_values(by='mydate')
Run Code Online (Sandbox Code Playgroud)
这让我认为我的子集方式应该更快。
mydate col 将始终采用25-DEC-2016 09:15:00格式(注意 DEC 而不是 Dec)我可以使用吗format='%d-%b-%Y %H:%M:%S'
首先,我建议pd.to_datetime在整个数组/系列上使用,所以它是:
pd.to_datetime(['10:15:00','11:15:00','12:15:00','13:15:00']).time
Run Code Online (Sandbox Code Playgroud)
而不是
[pd.to_datetime(i).time() for i in ['10:15:00','11:15:00','12:15:00','13:15:00']]
Run Code Online (Sandbox Code Playgroud)
其次,你对格式的看法是正确的。正如文档中所述,pd.to_datetime使用速度要快得多(x5-10 倍)
pd.to_datetime(['25-DEC-2016 09:15:00', '25-DEC-2016 09:15:00'],
format='%d-%b-%Y %H:%M:%S')
Run Code Online (Sandbox Code Playgroud)
而不是
pd.to_datetime(['25-DEC-2016 09:15:00', '26-DEC-2016 09:15:00'],
infer_datetime_format=True)
Run Code Online (Sandbox Code Playgroud)
现在考虑您的数据框:
df = pd.DataFrame({'col1': [1, 2, 3, 2],
'mydate': ['25-DEC-2016 09:15:00',
'25-DEC-2016 11:15:00',
'26-DEC-2016 11:15:00',
'26-DEC-2016 12:15:00']})
>>>
col1 mydate
0 1 25-DEC-2016 09:15:00
1 2 25-DEC-2016 11:15:00
2 3 26-DEC-2016 11:15:00
3 2 26-DEC-2016 12:15:00
Run Code Online (Sandbox Code Playgroud)
您可以首先转换mydate实际datetime系列中的列:
df['mydate'] = pd.to_datetime(df.mydate, format='%d-%b-%Y %H:%M:%S')
Run Code Online (Sandbox Code Playgroud)
然后您将能够通过访问器访问date和time字段(以及更多)dt:
df.mydate.dt.date
>>>
0 2016-12-25
1 2016-12-25
2 2016-12-26
3 2016-12-26
df.mydate.dt.time
>>>
0 09:15:00
1 11:15:00
2 11:15:00
3 12:15:00
Run Code Online (Sandbox Code Playgroud)
因此,在计算切片时,您可以使用:
for time in times:
slice = df[df.mydate.dt.time < time]
print(time, slice, sep='\n')
>>>
10:15:00
col1 mydate
0 1 2016-12-25 09:15:00
11:15:00
col1 mydate
0 1 2016-12-25 09:15:00
12:15:00
col1 mydate
0 1 2016-12-25 09:15:00
1 2 2016-12-25 11:15:00
2 3 2016-12-26 11:15:00
13:15:00
col1 mydate
0 1 2016-12-25 09:15:00
1 2 2016-12-25 11:15:00
2 3 2016-12-26 11:15:00
3 2 2016-12-26 12:15:00
Run Code Online (Sandbox Code Playgroud)
请注意,您获得的实际上并不是切片,因为它们具有重叠的记录,因此您可能需要使用类似于以下内容的内容:
for start, end in zip(times, times[1:]):
slice = df[(start <= df.mydate.dt.time) & (df.mydate.dt.time <= end)]
Run Code Online (Sandbox Code Playgroud)
最后一点,您尝试使用 for 循环完成的任务可以使用pandas 的group by操作来获得。您只需要准备一个mytime仅包含时间的列:
df['mytime'] = df.mydate.dt.time
groups = df.groupby('mytime')
for group_key, group_df in groups:
print(group_key, group_df, sep='\n')
>>>
09:15:00
col1 mydate mytime
0 1 2016-12-25 09:15:00 09:15:00
11:15:00
col1 mydate mytime
1 2 2016-12-25 11:15:00 11:15:00
2 3 2016-12-26 11:15:00 11:15:00
12:15:00
col1 mydate mytime
3 2 2016-12-26 12:15:00 12:15:00
Run Code Online (Sandbox Code Playgroud)
好处是您不需要对单个数据帧进行操作,但您可以同时对每个组应用相同的操作和聚合:
groups.size()
>>>
mytime
09:15:00 1
11:15:00 2
12:15:00 1
groups.sum()
>>>
col1
mytime
09:15:00 1
11:15:00 5
12:15:00 2
Run Code Online (Sandbox Code Playgroud)
我相信timedelta在 pandas 中工作更好 - 所以第一个split字符串列和选择转换时间:
df['mydate'] = pd.to_timedelta(df['mydate'].str.split().str[1])
print (df)
col1 mydate
0 1 09:15:00
1 2 10:14:00
2 3 10:16:00
3 4 10:18:56
4 2 11:14:00
5 2 10:16:00
Run Code Online (Sandbox Code Playgroud)
也转换list:
times=pd.to_timedelta(['10:15:00','11:15:00','12:15:00',
'13:15:00','14:15:00','15:15:00', '15:30:00'])
print (times)
TimedeltaIndex(['10:15:00', '11:15:00', '12:15:00', '13:15:00', '14:15:00',
'15:15:00', '15:30:00'],
dtype='timedelta64[ns]', freq=None)
Run Code Online (Sandbox Code Playgroud)
最后创建切片:
for time in times:
sl = df[df.mydate<time]
print (sl)
Run Code Online (Sandbox Code Playgroud)