Convert column to timestamp - Pandas Dataframe

You can try these as well. Try passing infer_datetime_format = True while reading the file.

if the above method fails try the following

df2 = pd.to_datetime(df.col1)

or

df2 = pd.to_datetime(df['col1'])
df2

Note the above methods will only convert the str to datetime format and return them in df2. In short df2 will have only the datetime format of str without a column name for it. If you want to retain other columns of the dataframe and want to give a header to the converted column you can try the following

df['col1_converetd'] = pd.to_datetime(df.col1)

or

df['col1_converetd'] = pd.to_datetime(df['col1'])

This is comforatble if you dont want to create a dataframe or want to refer the converted column in future together with other attributes of the dataframe.


For the first format you can simply pass to_datetime, for the latter you need to explicitly describe the date format (see the table of available directives in the python docs):

In [21]: df
Out[21]:
                   col1           col2
0  04-APR-2018 11:04:29  2018040415203

In [22]: pd.to_datetime(df.col1)
Out[22]:
0   2018-04-04 11:04:29
Name: col1, dtype: datetime64[ns]

In [23]: pd.to_datetime(df.col2, format="%Y%m%d%H%M%S")
Out[23]:
0   2018-04-04 15:20:03
Name: col2, dtype: datetime64[ns]