Reputation: 408
I have this
>>> start
'1905'
>>> end
'2003'
>>> p = pd.period_range(pd.to_datetime(start, format="%y%m"), pd.to_datetime(end, format="%y%m"), freq='M')
>>> p
PeriodIndex(['2019-05', '2019-06', '2019-07', '2019-08', '2019-09', '2019-10',
'2019-11', '2019-12', '2020-01', '2020-02', '2020-03'],
dtype='period[M]', freq='M')
I want to convert p to list of string which will contain period as
>>> p
['1905', '1906', '1907', '1908', '1909', '1910', '1911', '1912', '2001', '2002', '2003']
Format of string matters, it should be in same format as shown in eg('yymm') Can someone help me in achieving this cleanly?
Upvotes: 2
Views: 872
Reputation: 402553
You can use PeriodIndex.strftime
with '%y%m'
to indicate "YYMM" format:
p.strftime('%y%m')
# Index(['1905', '1906', '1907', '1908', '1909', '1910', '1911', '1912', '2001',
# '2002', '2003'],
# dtype='object')
Upvotes: 3