Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create a custom timerange and convert it to list

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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?

>Solution :

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']
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading