问题
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