close

How to change the datetime format in Pandas

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How to change the datetime format in Pandas in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How to change the datetime format in Pandas?

  1. How to change the datetime format in Pandas?

    You can use dt.strftime if you need to convert datetime to other formats (but note that then dtype of column will be object (string)):

  2. change the datetime format in Pandas

    You can use dt.strftime if you need to convert datetime to other formats (but note that then dtype of column will be object (string)):

Method 1

You can use dt.strftime if you need to convert datetime to other formats (but note that then dtype of column will be object (string)):

import pandas as pd

df = pd.DataFrame({'DOB': {0: '26/1/2016', 1: '26/1/2016'}})
print (df)
         DOB
0  26/1/2016 
1  26/1/2016

df['DOB'] = pd.to_datetime(df.DOB)
print (df)
         DOB
0 2016-01-26
1 2016-01-26

df['DOB1'] = df['DOB'].dt.strftime('%m/%d/%Y')
print (df)
         DOB        DOB1
0 2016-01-26  01/26/2016
1 2016-01-26  01/26/2016

Method 2

Changing the format but not changing the type:

df['date'] = pd.to_datetime(df["date"].dt.strftime('%Y-%m'))

Summery

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read