Python | Pandas DatetimeIndex.strftime()
Last Updated :
24 Dec, 2018
Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier.
Pandas DatetimeIndex.strftime()
function convert to Index using specified date_format. The function return an Index of formatted strings specified by date_format, which supports the same string format as the python standard library.
Syntax: DatetimeIndex.strftime(date_format)
Parameters :
date_format : Date format string (e.g. “%Y-%m-%d”).
Return : Index of formatted strings
Example #1: Use DatetimeIndex.strftime()
function to convert the given DatetimeIndex object to the specified format.
import pandas as pd
didx = pd.DatetimeIndex(start = '2000-01-15 08:00' , freq = 'Q' ,
periods = 4 , tz = 'Asia/Calcutta' )
print (didx)
|
Output :
Now we want to convert the given DatetimeIndex object to ('%B %d, %Y, %r')
format.
didx.strftime( '% B % d, % Y, % r' )
|
Output :
As we can see in the output, the function has changed the format of the DatetimeIndex object to the desired format.
Example #2: Use DatetimeIndex.strftime()
function to convert the given DatetimeIndex object to the specified format.
import pandas as pd
didx = pd.date_range(pd.Timestamp( "2000-01-15 08:00" ),
periods = 5 , freq = 'MS' )
print (didx)
|
Output :
Now we want to convert the given DatetimeIndex object to ('%B %Y, %r')
format.
didx.strftime( '% B % Y, % r' )
|
Output :
As we can see in the output, the function has changed the format of the DatetimeIndex object to the desired format.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...