将列解析为日期时间的最快方法 [英] Fastest way to parse a column to datetime in pandas
问题描述
我有以下具有40万行的数据框.
I have the following dataframe with more than 400 000 lines.
df = pd.DataFrame({'date' : ['03/02/2015 23:00',
'03/02/2015 23:30',
'04/02/2015 00:00',
'04/02/2015 00:30',
'04/02/2015 01:00',
'04/02/2015 01:30',
'04/02/2015 02:00',
'04/02/2015 02:30',
'04/02/2015 03:00',
'04/02/2015 03:30',
'04/02/2015 04:00',
'04/02/2015 04:30',
'04/02/2015 05:00',
'04/02/2015 05:30',
'04/02/2015 06:00',
'04/02/2015 06:30',
'04/02/2015 07:00']})
我正在尝试尽快解析csv文件在pandas中的日期列.我知道如何使用read_csv做到这一点,但这需要很多时间!另外,我尝试了以下方法,但效果却很慢:df['dateTimeFormat'] = pd.to_datetime(df['date'],dayfirst=True)
I am trying to parse the date column of a csv file in pandas as fast as possible. I know how to do it with read_csv but that takes a lot of time! Also, I have tried the following which works but which is also very slow: df['dateTimeFormat'] = pd.to_datetime(df['date'],dayfirst=True)
如何高效且快速地将date列解析为datetime?
How could I parse efficiently and in a really fast way the date column to datetime?
非常感谢您的帮助,
皮埃尔
推荐答案
您可以通过 http:/来定义datetime
的格式. /strftime.org/:
You can define format of datetime
s by http://strftime.org/:
df = pd.concat([df] * 1000, ignore_index=True)
%timeit df['dateTimeFormat1'] = pd.to_datetime(df['date'],dayfirst=True)
2.94 s ± 285 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
%timeit df['dateTimeFormat2'] = pd.to_datetime(df['date'],format='%d/%m/%Y %H:%M')
55 ms ± 1.47 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
这篇关于将列解析为日期时间的最快方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!