The Pandas PeriodIndex function strftime() used to convert index using specified date format. It returns an ndarray of formatted strings specified by formatted strings.
1 PeriodIndex.strftime(*args, **kwargs)
date_format : It is string that specifies the date format to convert. ie. '%Y-%m-%d'.
*args : It specifies the additional arguments to be passed to the function.
**kwargs : It specifies the additional keywords to be passed to the function.
1 import pandas as pd
2
3 pIndex = pd.period_range('2021-05-01 12:34:43', periods = 3, freq = 's')
4
5 print('The original PeriodIndex object :')
6 print(pIndex)
7
8 res = pIndex.strftime('%B %d, %Y, %r')
9 print('The string formatted time for each periods :')
10 print(res)
In the above example, a PeriodIndex object is created using period_range() function by passing a date, periods and freq. A strftime() function is called by formatted string that format each period with specified formatted string and assign result to the variable that will be printed on console.
1 The original PeriodIndex object :
2 PeriodIndex(['2021-05-01 12:34:43', '2021-05-01 12:34:44',
3 '2021-05-01 12:34:45'],
4 dtype='period[S]')
5
6 The string formatted time for each periods :
7 Index(['May 01, 2021, 12:34:43 PM', 'May 01, 2021, 12:34:44 PM',
8 'May 01, 2021, 12:34:45 PM'],
9 dtype='object')
1 %a Weekday, short version Wed
2 %A Weekday, full version Wednesday
3 %w Weekday as a number 0-6, 0 is Sunday 3
4 %d Day of month 01-31 31
5 %b Month name, short version Dec
6 %B Month name, full version December
7 %m Month as a number 01-12 12
8 %y Year, short version, without century 18
9 %Y Year, full version 2018
10 %H Hour 00-23 17
11 %I Hour 00-12 05
12 %p AM/PM PM
13 %M Minute 00-59 41
14 %S Second 00-59 08
15 %f Microsecond 000000-999999 548513
16 %z UTC offset +0100
17 %Z Timezone CST
18 %j Day number of year 001-366 365
19 %U Week of the year, Sunday as first day 00-53 52
20 %W Week of the year, Monday as first day 00-53 52
21 %c Local version of date and time Mon Dec 31 17:41:00 2018
22 %C Century 20
23 %x Local version of date 12/31/18
24 %X Local version of time 17:41:00
25 %% A % character %
26 %G ISO 8601 year 2018
27 %u ISO 8601 weekday (1-7) 1
28 %V ISO 8601 weeknumber (01-53) 01
Example 2
1 import pandas as pd
2
3 dIndex = pd.date_range('2012-01-01 12:30:32',
4 periods = 3, freq = 'QE')
5 pIndex = pd.PeriodIndex(dIndex)
6
7 print('The original PeriodIndex object :')
8 print(pIndex)
9
10 res = pIndex.strftime('%B %d, %Y, %r')
11 print('The string formatted time for each periods :')
12 print(res)
In the above example, a PeriodIndex object is created using date_range() function by passing a date, periods and freq. A strftime() function is called by formatted string that format each period with specified formatted string and assign result to the variable that will be print.
1 The original PeriodIndex object :
2 PeriodIndex(['2012Q1', '2012Q2', '2012Q3'], dtype='period[Q-DEC]')
3
4 The string formatted time for each periods :
5 Index(['March 31, 2012, 12:00:00 AM', 'June 30, 2012, 12:00:00 AM',
6 'September 30, 2012, 12:00:00 AM'],
7 dtype='object')
Related options for your search