Python | Pandas PeriodIndex.strftime

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 PeriodIndex.strftime() function return an array of formatted strings specified by date_format, which supports the same string format as the python standard library.
Syntax : PeriodIndex.strftime(date_format)
Parameters :
date_format : date format string (e.g. ā%Y-%m-%dā)Return : ndarray of formatted strings
Example #1: Use PeriodIndex.strftime() function to print the given PeriodIndex object in the specified date_format.
# importing pandas as pd import pandas as pd Ā Ā # Create the PeriodIndex object pidx = pd.PeriodIndex(start = '2004-11-11 02:45:21 ', Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā end = '2021-5-21 8:45:29', freq = 'Y') Ā Ā # Print the PeriodIndex object print(pidx) |
Output :
Now we will use the PeriodIndex.strftime() function to return the each period element in (ā%b. %d, %Y was a %Aā) format.
# return the PeriodIndex in specified format pidx.strftime('% b. % d, % Y was a % A') |
]
Output :
As we can see in the output, the PeriodIndex.strftime() function has returned an Index object containing each element of the given PeriodIndex object in the specified format.
Ā
Example #2: Use PeriodIndex.strftime() function to print the given PeriodIndex object in the specified date_format.
# importing pandas as pd import pandas as pd Ā Ā # Create the PeriodIndex object pidx = pd.PeriodIndex(start = '2016-10-12 11:12:02',Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā end = '2020-04-12 11:32:12', freq = 'Q') Ā Ā # Print the PeriodIndex object print(pidx) |
Output :
Now we will use the PeriodIndex.strftime() function to return the each period element in (ā%b-%Yā) format.
# return the PeriodIndex in specified format pidx.strftime('% b-% Y') |
]
Output :
As we can see in the output, the PeriodIndex.strftime() function has returned an Index object containing each element of the given PeriodIndex object in the specified format.




