Skip to content
Related Articles

Related Articles

How to change the Pandas datetime format in Python?
  • Last Updated : 11 Dec, 2020

Prerequisites: Pandas

The date-time default format is “YYYY-MM-DD”. Hence, December 8, 2020, in the date format will be presented as “2020-12-08”. The datetime format can be changed and by changing we mean changing the sequence and style of the format. 

Function used 

strftime() can change the date format in python. 

Syntax:

strftime(format) 

Where, format is a string representing the type of required date format. 



  • For year %y 
  • For month %m
  • For day %d 

Approach

  • Import module
  • Provide date
  • Change the format using above function

Example

Python3




# importing pandas as pd
import pandas as pd
  
# Creating the date series
date_sr = pd.Series(pd.date_range(
    '2019-12-31', periods=3, freq='M', tz='Asia/Calcutta'))
  
# Creating the index
ind = ['Day 1', 'Day 2', 'Day 3']
  
# set the index
date_sr.index = ind
change_format = date_sr.dt.strftime('%d,%m,%Y')
  
# Print the formated date
print(change_format)

Output

Example

Python3




# importing pandas as pd
import pandas as pd
  
# change in date time format
date_sr = pd.to_datetime(pd.Series("2020-12-08"))
change_format = date_sr.dt.strftime('%d/%m/%Y')
  
# Print the formated date
print(change_format)

Output

Example

Python3




# importing pandas as pd
import pandas as pd
  
# change in date time format
date_sr = pd.to_datetime(pd.Series("2012-09-02"))
change_format = date_sr.dt.strftime('%d-%m-%Y')
  
# Print the formated date
print(change_format)

Output

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course

My Personal Notes arrow_drop_up
Recommended Articles
Page :