Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert column to timestamp - Pandas Dataframe

I have a Pandas Dataframe that has date values stored in 2 columns in the below format:

Column 1: 04-APR-2018 11:04:29 Column 2: 2018040415203  

How could I convert this to a time stamp. Datatype of both these column is Object.

like image 642
Taukheer Avatar asked Apr 29 '18 18:04

Taukheer


People also ask

How do I add a timestamp to pandas DataFrame?

Example 1: Timestamps with to_datetime. Here we are converting the CSV file into a dataframe using pandas. DataFrame() method after reading the contents of the file using pandas. read_csv(), the timestamps column from the data Dataframe is given as an argument in the to_datetime() for it to be converted into DateTime.


2 Answers

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] 
like image 140
Andy Hayden Avatar answered Sep 20 '22 06:09

Andy Hayden


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.

like image 43
Natty Avatar answered Sep 20 '22 06:09

Natty