0

I have a panda date list created from the code below.

import pandas as pd
panda_datelist = pd.bdate_range(pd.datetime.today(), periods=3).tolist()

I would like to convert panda_datelist into a python list. It should look something like this;

python_datelist_from_panda = ['10/2/2017','10/3/2017','10/4/2017']

I am using python v3.6

1 Answer 1

1

Use list comprehension

In [4553]: [x.strftime('%m/%d/%Y') for x in panda_datelist]
Out[4553]: ['10/20/2017', '10/23/2017', '10/24/2017']

Or, use map

In [4554]: map(lambda x: x.strftime('%m/%d/%Y'), panda_datelist)
Out[4554]: ['10/20/2017', '10/23/2017', '10/24/2017']

But, before converting to list you can actually use strftime

In [4559]: pd.bdate_range(pd.datetime.today(), periods=3).strftime('%m/%d/%Y')
Out[4559]:
array([u'10/20/2017', u'10/23/2017', u'10/24/2017'],
      dtype='<U10')

Details

In [4555]: panda_datelist
Out[4555]:
[Timestamp('2017-10-20 00:00:00', freq='B'),
 Timestamp('2017-10-23 00:00:00', freq='B'),
 Timestamp('2017-10-24 00:00:00', freq='B')]
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I didn't know strftime works for panda. Panda gives me the impression of being a totally different animal from python data structures.
I'm trying to understand the solution of using map and lambda. They look pretty foreign to me. Is it some kind of functional programming?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.