I want to make a list like this:
list = ['200501','200502','200503'....'202012']
I want I could hard code the list from 200501(jan 2005) to 202012(dec 2020) but I would like to do it in a smarter way.
I tried:
l = pd.date_range(start='2005-01', end='2021-01', freq='m')
and I get a <class 'pandas.core.indexes.datetimes.DatetimeIndex'>
with the dates I want.
But now I want to transform it to a list and change 2005-01-31
(that the output format i got) to 200501
my expected result is list = ['200501','200502','200503'....'202012']
I tried to cast l to list but if fails.
What's the best approach to do it?
CodePudding user response:
You could use strftime
:
out = pd.date_range(start='2005-01', end='2021-01', freq='m').strftime('%Y%m').tolist()
Output:
['200501',
'200502',
'200503',
'200504',
...
'202010',
'202011',
'202012']