Speed up date columns conversion (pandas) from string to datetime

别等时光非礼了梦想. 提交于 2020-05-17 07:56:29

问题


I am working with a large .csv file in python and its date column is 'str'. I am using the following code to convert the records in this column to datetime.

df[date_column].fillna('1900-01-01',inplace=True)
df[date_column] = df[date_column].apply(lambda x : pd.to_datetime(x, format = datetime_format))

But this seems to be taking quite a long time to execute. Any suggestions on how to handle this is welcomed. Thanks.


回答1:


When you read your csv , you can using parse_dates

df = pd.read_csv('yourcsv.csv',parse_dates = date_column)

Then let us using converters

pd.read_csv('yourcsv.csv', converters={'date_column':lambda x : pd.to_datetime(x,errors = 'coerce')})


来源:https://stackoverflow.com/questions/51392909/speed-up-date-columns-conversion-pandas-from-string-to-datetime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!