Convert string column to datetime in Pandas

In this post, we are going to understand how to convert string columns to datetime in pandas python with the help of examples. We are going to learn about some built-in methods that it can be used on single or multiple columns.

1. pd.to_datetime() to Convert string column to datetime in Pandas


In this example, we have imported the Pandas module in python by using import pandas as pd. We are using the pandas to_datetime() method to convert a date as a string to date. Let us understand with the below example how to convert string columns to date in python.

Steps to convert string column to datetime in pandas

  • Import pandas module import pandas as pd
  • call to_datetime() method by passing the column name
  • check the type of column by using dtypes property
  • Print the final result using print() method

Python Program Example

import pandas as pd


dfobj = pd.DataFrame({'Date':['11/8/2014', '21/04/2020', '10/2/2017'],
                'Name':['rack', 'David', 'Max'],
                'Fee':[12000, 15000, 15000]})


print('before conversion :\n',dfobj.dtypes)


dfobj['Date']= pd.to_datetime(dfobj['Date'])

print('\n string to date conversion:\n',dfobj.dtypes)

Output

before conversion :
 Date    object
Name    object
Fee      int64
dtype: object

 string to date conversion:
 Date    datetime64[ns]
Name            object
Fee              int64
dtype: object

2. pd.to_datetime() to convert string to datetime specific format


In the above example, we have used the pandas to_datetime() method to convert a given string date to date. Sometimes need to convert a date to a specific format so we can use to_datetime() method format argument to specific in which format we want to convert a date. So in the below example, we have passed format=’%Y%m%d.

Important Note :The given string date must match with a specific date format otherwise exception will raise.

Python Program Example

import pandas as pd


dfobj = pd.DataFrame({'Date':['20210812', '20210813', '20200810'],
                'Name':['rack', 'David', 'Max'],
                'Fee':[12000, 15000, 15000]})





dfobj['Date']= pd.to_datetime(dfobj['Date'],format='%Y%m%d')

print(dfobj)

print('\n string to date conversion:\n',dfobj.dtypes)


Output

        Date   Name    Fee
0 2021-08-12   rack  12000
1 2021-08-13  David  15000
2 2020-08-10    Max  15000

string to date conversion:
 Date    datetime64[ns]
Name            object
Fee              int64
dtype: object

3.Convert multiple columns to date in Pandas


In this example instead of single, we are converting multiple columns in which date is given as string to date by using the to_datetime() to specific format by passing the format = ‘%Y%m%d’.

Python Program to convert multiple columns to in Pandas

import pandas as pd


dfobj = pd.DataFrame({'JoinDate':['20210812', '20210813', '20200810'],                      
                'Name':['rack', 'David', 'Max'],
                'Fee':[12000, 15000, 15000],
                'BirthDate':['20210812', '20210813', '20200810']})





dfobj['JoinDate']= pd.to_datetime(dfobj['JoinDate'],format='%Y%m%d')
dfobj['BirthDate']= pd.to_datetime(dfobj['BirthDate'],format='%Y%m%d')

print(dfobj)

print('\n string to date conversion:\n',dfobj.dtypes)


Output

    JoinDate   Name    Fee  BirthDate
0 2021-08-12   rack  12000 2021-08-12
1 2021-08-13  David  15000 2021-08-13
2 2020-08-10    Max  15000 2020-08-10

string to date conversion:
JoinDate     datetime64[ns]
Name                 object
Fee                   int64
BirthDate    datetime64[ns]
dtype: object

4.Datafrmae.astype() to Convert string column to datetime


In this example we are using astype() method of python pandas datetframe to convert given date as string to date. The astype() takes a type as an argument and change the column to passed type herein below example we are passed datetime64[ns] as a type from the date column.

Python Program

import pandas as pd


dfobj = pd.DataFrame({'Date':['11/8/2014', '21/04/2020', '10/2/2017'],
                'Name':['rack', 'David', 'Max'],
               'BirthDate':['20210812', '20210813', '20200810'],
                'Fee':[12000, 15000, 15000]})

                })


print('before conversion :\n',dfobj.dtypes)




dfobj['Date']= dfobj['Date'].astype('datetime64[ns]')
dfobj['BirthDate']= dfobj['BirthDate'].astype('datetime64[ns]')

print('\n after conversion:\n',dfobj.dtypes)

Output

before conversion :
 Date    object
Name    object
Fee      int64
dtype: object

 after conversion:
 Date    datetime64[ns]
Name            object
Fee              int64
dtype: object

5. Datafrmae.astype() to Convert string mutiple columns to datetime


In this example we are using astype() method of python pandas datetframe to convert multiple given date as string column to date.

Python Program

import pandas as pd


dfobj = pd.DataFrame({'Date':['11/8/2014', '21/04/2020', '10/2/2017'],
                'Name':['rack', 'David', 'Max'],
               'BirthDate':['20210812', '20210813', '20200810'],
                'Fee':[12000, 15000, 15000]})




dfobj['Date']= dfobj['Date'].astype('datetime64[ns]')
dfobj['BirthDate']= dfobj['BirthDate'].astype('datetime64[ns]')

print(dfobj)

print('\n after conversion:\n',dfobj.dtypes)


Output

        Date   Name  BirthDate    Fee
0 2014-11-08   rack 2021-08-12  12000
1 2020-04-21  David 2021-08-13  15000
2 2017-10-02    Max 2020-08-10  15000

 after conversion:
 Date         datetime64[ns]
Name                 object
BirthDate    datetime64[ns]
Fee                   int64
dtype: object

Summary

In this post we have learned 5 ways of how to Convert string column to datetime in Pandas using pandas pd.to_datetime() method and dataframe.astype() that can be used with single or multiple columns.