Reputation: 1126
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?
Upvotes: 2
Views: 819
Reputation: 862641
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']
Upvotes: 3