pandas 读取 .csv 文件时在 Python 中解析日期的最快方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38645733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
The fastest way to parse dates in Python when reading .csv file?
提问by Cofeinnie Bonda
I have a .csv file that has 2 separate columns for 'Date'
and ' Time'
. I read the file like this:
我有一个 .csv 文件,其中有 2 个单独的列'Date'
和' Time'
。我像这样读取文件:
data1 = pd.read_csv('filename.csv', parse_dates=['Date', 'Time'])
But it seems that only the ' Date'
column is in time format while the 'Time'
column is still string or in a format other than time format.
但似乎只有' Date'
列是时间格式,而'Time'
列仍然是字符串或时间格式以外的格式。
When I do the following:
当我执行以下操作时:
data0 = pd.read_csv('filename.csv')
data0['Date'] = pd.to_datetime(data0['Date'])
data0['Time'] = pd.to_datetime(data0['Time'])
It gives a dataframe I want, but takes quite some time. So what's the fastest way to read in the file and convert the date and time from a string format?
它提供了我想要的数据框,但需要相当长的时间。那么读入文件并从字符串格式转换日期和时间的最快方法是什么?
The .csv file is like this:
.csv 文件是这样的:
Date Time Open High Low Close
0 2004-04-12 8:31 AM 1139.870 1140.860 1139.870 1140.860
1 2005-04-12 10:31 AM 1141.219 1141.960 1141.219 1141.960
2 2006-04-12 12:33 PM 1142.069 1142.290 1142.069 1142.120
3 2007-04-12 3:24 PM 1142.240 1143.140 1142.240 1143.140
4 2008-04-12 5:32 PM 1143.350 1143.589 1143.350 1143.589
Thanks!
谢谢!
回答by RAVI
Here, In your case 'Time' is in AM/PMformat which take more time to parse.
在这里,在您的情况下,“时间”采用AM/PM格式,这需要更多时间来解析。
You can add formatto increase speed of to_datetime() method.
您可以添加格式以提高 to_datetime() 方法的速度。
data0=pd.read_csv('filename.csv')
# %Y - year including the century
# %m - month (01 to 12)
# %d - day of the month (01 to 31)
data0['Date']=pd.to_datetime(data0['Date'], format="%Y/%m/%d")
# %I - hour, using a -hour clock (01 to 12)
# %M - minute
# %p - either am or pm according to the given time value
# data0['Time']=pd.to_datetime(data0['Time'], format="%I:%M %p") -> around 1 sec
data0['Time']=pd.datetools.to_time(data0['Time'], format="%I:%M %p")
For more methods info : Pandas Tools
更多方法信息:Pandas Tools
For more format options check - datetime format directives.
有关更多格式选项,请检查日期时间格式指令。
For 500K rows it improved speed from around 60 seconds -> 0.01 seconds in my system.
对于 500K 行,它在我的系统中将速度从大约 60 秒 -> 0.01 秒提高了。
You can also use :
您还可以使用:
# Combine date & time directly from string format
pd.Timestamp(data0['Date'][0] + " " + data0['Time'][0])