2

I am trying to create a list L of dates from '2020-01-22' to '2020-01-25' (very small example...). So I expect to get:

print(L)
['2020-01-22', '2020-01-23', '2020-01-24', '2020-01-25']

I used (because I thought it was enough):

import pandas as pd
from datetime import datetime
L = pd.date_range(start='2020-01-22',end='2020-01-25').tolist()

But this gives me:

[Timestamp('2020-01-22 00:00:00', freq='D'),
 Timestamp('2020-01-23 00:00:00', freq='D'),
 Timestamp('2020-01-24 00:00:00', freq='D'),
 Timestamp('2020-01-25 00:00:00', freq='D')]

I really have a problem understanding how datetime works; should I import something else? What would be a proper simple coding to have in my list just the dates '2020-01-22', etc. in that precise format?

1
  • 3
    list(pd.date_range(start='2020-01-22',end='2020-01-25').strftime('%Y-%m-%d')) Commented Apr 22, 2020 at 15:01

1 Answer 1

3

You need converting to strings by DatetimeIndex.strftime:

L = pd.date_range(start='2020-01-22',end='2020-01-25').strftime('%Y-%m-%d').tolist()
print (L)
['2020-01-22', '2020-01-23', '2020-01-24', '2020-01-25']

Here also working converting to strings (I think because 00:00:00 times):

L = pd.date_range(start='2020-01-22',end='2020-01-25').astype(str).tolist()
print (L)
['2020-01-22', '2020-01-23', '2020-01-24', '2020-01-25']
Sign up to request clarification or add additional context in comments.

1 Comment

Great thanks to you, python specialists ! So simple, but I could have spent hours on it...

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.