1

I have the following dataframe with more than 400 000 lines.

df = pd.DataFrame({'date' : ['03/02/2015 23:00',
'03/02/2015 23:30',
'04/02/2015 00:00',
'04/02/2015 00:30',
'04/02/2015 01:00',
'04/02/2015 01:30',
'04/02/2015 02:00',
'04/02/2015 02:30',
'04/02/2015 03:00',
'04/02/2015 03:30',
'04/02/2015 04:00',
'04/02/2015 04:30',
'04/02/2015 05:00',
'04/02/2015 05:30',
'04/02/2015 06:00',
'04/02/2015 06:30',
'04/02/2015 07:00']})

I am trying to parse the date column of a csv file in pandas as fast as possible. I know how to do it with read_csv but that takes a lot of time! Also, I have tried the following which works but which is also very slow: df['dateTimeFormat'] = pd.to_datetime(df['date'],dayfirst=True)

How could I parse efficiently and in a really fast way the date column to datetime?

Thank you very much for your help,

Pierre

Vivek Kalyanarangan
  • 8,951
  • 1
  • 23
  • 42
Peslier53
  • 587
  • 1
  • 7
  • 21

1 Answers1

10

You can define format of datetimes by http://strftime.org/:

df = pd.concat([df] * 1000, ignore_index=True)


%timeit df['dateTimeFormat1'] = pd.to_datetime(df['date'],dayfirst=True)
2.94 s ± 285 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)

%timeit df['dateTimeFormat2'] = pd.to_datetime(df['date'],format='%d/%m/%Y %H:%M') 
55 ms ± 1.47 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252