How do I convert strings in a Pandas data frame to a 'date' data type? How do I convert strings in a Pandas data frame to a 'date' data type? pandas pandas

How do I convert strings in a Pandas data frame to a 'date' data type?


Use astype

In [31]: dfOut[31]:    a        time0  1  2013-01-011  2  2013-01-022  3  2013-01-03In [32]: df['time'] = df['time'].astype('datetime64[ns]')In [33]: dfOut[33]:    a                time0  1 2013-01-01 00:00:001  2 2013-01-02 00:00:002  3 2013-01-03 00:00:00


Essentially equivalent to @waitingkuo, but I would use pd.to_datetime here (it seems a little cleaner, and offers some additional functionality e.g. dayfirst):

In [11]: dfOut[11]:   a        time0  1  2013-01-011  2  2013-01-022  3  2013-01-03In [12]: pd.to_datetime(df['time'])Out[12]:0   2013-01-01 00:00:001   2013-01-02 00:00:002   2013-01-03 00:00:00Name: time, dtype: datetime64[ns]In [13]: df['time'] = pd.to_datetime(df['time'])In [14]: dfOut[14]:   a                time0  1 2013-01-01 00:00:001  2 2013-01-02 00:00:002  3 2013-01-03 00:00:00

Handling ValueErrors
If you run into a situation where doing

df['time'] = pd.to_datetime(df['time'])

Throws a

ValueError: Unknown string format

That means you have invalid (non-coercible) values. If you are okay with having them converted to pd.NaT, you can add an errors='coerce' argument to to_datetime:

df['time'] = pd.to_datetime(df['time'], errors='coerce')


I imagine a lot of data comes into Pandas from CSV files, in which case you can simply convert the date during the initial CSV read:

dfcsv = pd.read_csv('xyz.csv', parse_dates=[0]) where the 0 refers to the column the date is in.
You could also add , index_col=0 in there if you want the date to be your index.

See https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_csv.html