在对San Fransisco的Police Department Incident Reports 历史数据整理时发现,2018年5⽉之前的数据中将⽇期和时间分开为了Date和Time两个列,⽽2018年之后的数据则只有Datetime⼀列,需要进⾏格式统⼀。
源数据⽂件中Date⽇期与Time时间分为了两列
r ea d_c sv命令中的pa r _da tes参数详解
由于csv⽂件中⽇期和时间被分为了两列,pd.read_csv命令读取⽂件时,需指定par_dates = [ ['Date', 'Time'] ],亦即将[ ['Date', 'Time'] ]两列的字符串先合并后解析⽅可。合并后的新列会以下划线'_'连接原列名命名,本例中列名为'Date_Time'。解析得到的⽇期格式列会作为DataFrame的第⼀列,在index_col指定表格中的第⼏列作为Index时需要⼩⼼。如本例中,指定参数index_col = 0,则此时会以新⽣曾的Date_Time列⽽不是IncidntNum作为Index。因此保险的⽅法是指定列名,如index_col = 'IncidntNum'。
read_csv指定par_dates = [ ['Date', 'Time'] ]运⾏结果
美国打叙利亚
如果写成了par_dates = ['Date', 'Time'] ,pd. read_csv()会分别对'Date', 'Time'进⾏字符串转⽇期,此外还会造成⼀个⼩⼩的⿇烦。由于本例中的Time时间列格式为‘HH:MM’,par_dates 默认调⽤dateutil.parr.par解析为Datetime时,在解析Time这⼀列时,会⾃作主张
在前⾯加上⼀个当前⽇期。
read_csv指定par_dates = ['Date', 'Time']运⾏结果
⽽且,read_csv指定par_dates会使得读取csv⽂件的时间⼤⼤增加。该⽂件共220.5万条数据,不进⾏⽇期时间的解析时仅需不到10秒时间
读取并转为DataFrame,读取同时解析⽇期竟则需要接近10分钟。仔细查阅了read_csv⽂档,发现当指定infer_datetime_format = True时,
pandas会推荐⽇期字符串的格式从⽽使得解析速度加速5-10倍(原⽂如下)。
作文感恩infer_datetime_format : bool, default Fal
If True and par_dates is enabled, pandas will attempt to infer the format of the datetime strings in the columns, and if it can be inferred, switch to a faster method of parsing them. In some cas this can政治理论学习心得体会
increa the parsing speed by 5-10x.
实际运⾏下来加速效果明显,由原来的超过9分钟减少到了38.4秒。真心话大冒险惩罚
此外,keep_date_col 参数则是⽤了指定解析为⽇期格式的列是否保留。⽂档中提到dayfirst ⽤来区分类似‘10/11/12’的字符串表⽰的到底是'dd/mm/yy'还是'mm/dd/yy',美国习惯'MM/DD/YYYY',但国际通⽤更常⽤的是'DD/MM/YYYY'。如果涉及到时区等,还需指定
date_parr为'_datetime() 并指定'utc=True',具体参见Parsing a CSV with mixed timezones,此处不赘述。
infer_datetime_format = True可显著减少read_csv命令⽇期解析时间
tetime.strptime()⽐较
<_da tetim e()、da teutil.pa rr.pa r()与da tetime.strptim e()⽐较
服务明星
datetime.strptime()
datetime是Python处理⽇期和时间的标准库。datetime.strptime()是由字符串格式转化为⽇期格式的函数。
datetime.strptime()
函数执⾏需指定字符串的⽇期表⽰的格式,如下图所⽰。如果格式不匹配,如按照‘%d %m %y’格式解析‘21/04/2018’会报错(ValueError: time data '21/04/2018' does not match format '%d %m %Y')
Dir
ect ive Description
Example
Output
%a Weekday as locale’s abbreviated name.Sun, Mon, …, Sat
%A Weekday as locale’s full name.Sunday, Monday, …, Saturday
%w Weekday as a decimal number, where 0 is Sunday and 6 is Saturday.0, 1, 2, 3, 4, 5, 6
%d Day of the month as a zero-padded decimal number.01, 02, …, 31
%b Month as locale’s abbreviated name.Jan, Feb, …, Dec January,
dateutil.parr.par()运⾏结果
<_datetime()
话术大全
<_datetime()也可以⾃动解析多种格式的⽇期,但⽐par()谨慎,或者可以说,pd.to_datetime()的灵活性介于dateutil.parr.par()与datetime.strptime()之间。除常规的'dayfirst'、'yearfirst'来区分类似'10/11/12'这类字符串中'年⽉⽇'的排列顺序之外,pd.to_datetime()执⾏时可通过'format'参数来指定具体的某⼀⽇期格式。'exact'参数则是指定是否强⾏查找出符合'format'格式的字符串来进⾏识别。
<_datetime运⾏结果
还是利⽤csv⽂件中Date、Time两列数据,先将字符串合并,然后分别⽤datetime.strptime()、dateutil.parr.par()与pd.to_datetime()解析,结果如图所⽰。最快的datetime.strptime()只⽤了1分14秒完成了220.5万条数据的⽇期解析;dateutil.parr.par()⽤了3分56
秒,pd.to_datetime()居然⽤了18分34秒之多。pd.to_datetime()指定'format'参数后⽤时有所改善,但仍然⽤了10分36秒才完成。
鸭舌帽怎么洗
<_datetime()参数中有⼀个与read_csv()命令相同的参数'infer_datetime_format',但在这⾥指定infer_datetime_format = True似乎对运⾏速度没有影响。换个时间再试运⾏时间会有差异,但三者的
速度排名不变。⽽且,这样看来最⾼效的⽅式反⽽是在read_csv()时就将⽇期解成长记录册模板
datetime.strptime()、dateutil.parr.par()与pd.to_datetime()速度对⽐