In Pandas how do I convert a string of date strings to datetime objects and put them in a DataFrame?
import pandas as pd
date_stngs = ('2008-12-20','2008-12-21','2008-12-22','2008-12-23')
a = pd.Series(range(4),index = (range(4)))
for idx, date in enumerate(date_stngs):
a[idx]= pd.to_datetime(date)
This code bit produces error:
TypeError:" 'int' object is not iterable"
Can anyone tell me how to get this series of date time strings into a DataFrame as DateTime
objects?
Solution 1:
>>> import pandas as pd
>>> date_stngs = ('2008-12-20','2008-12-21','2008-12-22','2008-12-23')
>>> a = pd.Series([pd.to_datetime(date) for date in date_stngs])
>>> a
0 2008-12-20 00:00:00
1 2008-12-21 00:00:00
2 2008-12-22 00:00:00
3 2008-12-23 00:00:00
UPDATE
Use pandas.to_datetime(pd.Series(..)). It's concise and much faster than above code.
>>> pd.to_datetime(pd.Series(date_stngs))
0 2008-12-20 00:00:00
1 2008-12-21 00:00:00
2 2008-12-22 00:00:00
3 2008-12-23 00:00:00
Solution 2:
In [46]: pd.to_datetime(pd.Series(date_stngs))
Out[46]:
0 2008-12-20 00:00:00
1 2008-12-21 00:00:00
2 2008-12-22 00:00:00
3 2008-12-23 00:00:00
dtype: datetime64[ns]
Update: benchmark
In [43]: dates = [(dt.datetime(1960, 1, 1)+dt.timedelta(days=i)).date().isoformat() for i in range(20000)]
In [44]: timeit pd.Series([pd.to_datetime(date) for date in dates])
1 loops, best of 3: 1.71 s per loop
In [45]: timeit pd.to_datetime(pd.Series(dates))
100 loops, best of 3: 5.71 ms per loop