cJc
cJc

Reputation: 863

How to change the date format in a list of dates

I want to import data from multiple csv files. Every business day a new CSV file is produces and the first 8 digits of the file name is the date in question. I want to import the data from the last 100 files every day. So, I thought I wanted to make a list of dates and use that in a for loop to collect all the data. To create a date range I did the following:

>>> datelist = pd.bdate_range(end=pd.datetime.today(), periods = 100).tolist
>>> datelist
<bound method DatetimeIndex.tolist of DatetimeIndex(['2015-07-07', '2015-07-08', '2015-07-09', '2015-07-10',
 dtype='datetime64[ns]', freq='B')>

Now, how do I change the date format from yyyy-mm-dd to yyyymmdd ??

Upvotes: 1

Views: 5065

Answers (1)

Alexander
Alexander

Reputation: 109546

You can use a list comprehension together with strftime.

datelist = [d.strftime('%Y%m%d') 
            for d in pd.bdate_range(end=pd.datetime.today(), periods = 100)]

>>> datelist
['20151110',
 '20151111',
 '20151112',
 '20151113',
 '20151116',
 '20151117',
 '20151118',
 '20151119',
 '20151120',
 '20151123', ...]

Upvotes: 2

Related Questions