Convert Multiple columns to datetime in Pandas

Pandas

In this post, we are going to understand how to Convert Multiple columns to datetime in Pandas python by using the built-in to_datetime() and astype() method in our examples.

1.Convert multiple columns to datetime in Pandas


In this example instead of a single column, we are converting multiple columns in which data is given as a 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

2. 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 dates as string to datetime and finally checking the dataframe data type using dfobj.dtypes property.

Python Program

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]})




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    Fee
0 2021-09-08 09:35:04   rack  12000
1 2021-09-09 09:32:04  David  15000
2 2021-06-06 08:33:04    Max  15000

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

3. Convert mutiple column timestamp to datetime


In this example, we have timestamp column pandas data frame ‘Date’ column and convert it to datetime with format ‘%Y%m%d-%H%M%S’.

Python Program Example

import pandas as pd


dfobj = pd.DataFrame({'Date':['2021090809354', '2021090909324', '2021060608334'],
                'Name':['rack', 'David', 'Max'],               
                'Fee':[12000, 15000, 15000]})



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




print(dfobj)

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


output

                 Date   Name    Fee
0 2021-09-08 07:35:04   rack  12000
1 2021-09-09 06:32:04  David  15000
2 2021-06-06 08:33:04    Max  15000

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

4. Convert timestamp of multiple columns to datetime


In this example, we have timestamp column pandas data frame ‘Date’ and BirthDate columns and convert it to datetime with format ‘%Y%m%d-%H%M%S’ by using pandas method to_datetime().

Python Program Example

import pandas as pd


dfobj = pd.DataFrame({'Date':['2021090807354', '2021090906324', '2021060608334'],
                'Name':['rack', 'David', 'Max'],'Fee':[12000, 15000, 15000],
               'BirthDate':['2020090807354', '2020090906324', '2020060608334']
                })



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


dfobj['BirthDate']= pd.to_datetime(dfobj['BirthDate'], format='%Y%m%d%H%M%S')
print(dfobj)

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

output

                 Date   Name    Fee           BirthDate
0 2021-09-08 07:35:04   rack  12000 2020-09-08 07:35:04
1 2021-09-09 06:32:04  David  15000 2020-09-09 06:32:04
2 2021-06-06 08:33:04    Max  15000 2020-06-06 08:33:04

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

Summary

In this post we have learned 4 ways of how to convert Multiple columns to datetime in Pandas using pd.to_datetime() method and dataframe.astype().