How to change the Pandas datetime format in Python?
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
import pandas as pd
date_sr = pd.Series(pd.date_range(
'2019-12-31' , periods = 3 , freq = 'M' , tz = 'Asia/Calcutta' ))
ind = [ 'Day 1' , 'Day 2' , 'Day 3' ]
date_sr.index = ind
change_format = date_sr.dt.strftime( '%d,%m,%Y' )
print (change_format)
|
Output
Example
Python3
import pandas as pd
date_sr = pd.to_datetime(pd.Series( "2020-12-08" ))
change_format = date_sr.dt.strftime( '%d/%m/%Y' )
print (change_format)
|
Output
Example
Python3
import pandas as pd
date_sr = pd.to_datetime(pd.Series( "2012-09-02" ))
change_format = date_sr.dt.strftime( '%d-%m-%Y' )
print (change_format)
|
Output
Last Updated :
04 Jul, 2022
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...