Lauren Miller
Lauren Miller

Reputation: 11

Trying to Create a List with Dates and Times in Python without Time Stamps

I am trying to use Astropy to determine a coordinate on a date/time. I want to create a list that includes dates and times increasing by the hour. For example:

[2018-01-01 00:00:00,2018-01-01 01:00:00,2018-01-01 02:00:00]

However, I need to have the dates be from 2018-01-01 to 2019-08-31 and include hours 0 to 24, i.e.:

[2018-01-01 00:00:00,2018-01-01 01:00:00,2018-01-01 02:00:00,....2018-01-01 
23:00:00,2018-01-02 00:00:00,2018-01-02 01:00:00,2018-01-02 02:00:00,....
2018-01-02 23:00:00,...]

This is what I have code thus far :

import pandas as pd 
import numpy as np

dates=[]
for i in np.arange(0,24):
    d=pd.date_range('2018-01-01', periods=243) + pd.Timedelta(hours=i)
    dates.append(d)


tim=[]
for j,k in zip(np.arange(0,24),np.arange(0,243)):
    t=dates[j][k]
    tim.append(t)

print(tim)

which returns:

[Timestamp('2018-01-01 00:00:00', freq='D'), Timestamp('2018-01-02 01:00:00', 
freq='D'), Timestamp('2018-01-03 02:00:00', freq='D'), Timestamp('2018-01-04 
03:00:00', freq='D'), Timestamp('2018-01-05 04:00:00', freq='D'), 
Timestamp('2018-01-06 05:00:00', freq='D'), Timestamp('2018-01-07 06:00:00', 
freq='D'), Timestamp('2018-01-08 07:00:00', freq='D'), Timestamp('2018-01-09 
08:00:00', freq='D'), Timestamp('2018-01-10 09:00:00', freq='D'), 
Timestamp('2018-01-11 10:00:00', freq='D'), Timestamp('2018-01-12 11:00:00', 
freq='D'), Timestamp('2018-01-13 12:00:00', freq='D'), Timestamp('2018-01-14 
13:00:00', freq='D'), Timestamp('2018-01-15 14:00:00', freq='D'), 
Timestamp('2018-01-16 15:00:00', freq='D'), Timestamp('2018-01-17 16:00:00', 
freq='D'), Timestamp('2018-01-18 17:00:00', freq='D'), Timestamp('2018-01-19 
18:00:00', freq='D'), Timestamp('2018-01-20 19:00:00', freq='D'), 
Timestamp('2018-01-21 20:00:00', freq='D'), Timestamp('2018-01-22 21:00:00', 
freq='D'), Timestamp('2018-01-23 22:00:00', freq='D'), Timestamp('2018-01-24 
23:00:00', freq='D')]

Please help! Thank you.

Upvotes: 1

Views: 323

Answers (2)

Chris Adams
Chris Adams

Reputation: 18647

To get the list in your desired format you could use:

[str(ts) for ts in pd.date_range('2018-01-01', '2019-08-31', freq='H')]

Upvotes: 0

John Zwinck
John Zwinck

Reputation: 249153

It's simple:

pd.date_range('2018-01-01', '2019-09-01', freq='H')

Upvotes: 1

Related Questions