Skip to content
Advertisement

Pandas slow to merge and convert to datetime

I have two columns of data in a DataFrame containing a date and a time. Both start as strings. I want them to end up merged as a single column in datetime format.

The head of the DataFrame is:

JavaScript

They are in a DF called df_flattened and has about 20k rows and the code I am currently using is:

JavaScript

However, this takes about 2.6s to run and the dataset is going to get a lot bigger in the future. Can anyone suggest a fast way of doing this?

Advertisement

Answer

You can use + for join columns instead apply:

JavaScript

Also is possible specify format of joined datetimes:

JavaScript

Performance for 20k rows:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement