So I am looking for a way to fill an empty dataframe column with hourly values between two dates. for example between
StartDate = 2019:01:01 00:00:00
to
EndDate = 2019:02:01 00:00:00
I would want a column that has
2019:01:01 00:00:00,2019:01:01 01:00:00,2019:02:01 00:00:00...
in Y:M:D H:M:S format. I am not sure what the most efficient way of doing this is, is there a way to do it via pandas or would you have to use a for loop over a given timedelta between a range for eg?
`
CodePudding user response:
Use date_range
with DataFrame
constructor:
StartDate = '2019-01-01 00:00:00'
EndDate = '2019-02-01 00:00:00'
df = pd.DataFrame({'dates':pd.date_range(StartDate, EndDate, freq='H')})
If there is custom format of dates first convert them to datetimes:
StartDate = '2019:01:01 00:00:00'
EndDate = '2019:02:01 00:00:00'
StartDate = pd.to_datetime(StartDate, format='%Y:%m:%d %H:%M:%S')
EndDate = pd.to_datetime(EndDate, format='%Y:%m:%d %H:%M:%S')
df = pd.DataFrame({'dates':pd.date_range(StartDate, EndDate, freq='H')})
print (df.head(10))
dates
0 2019-01-01 00:00:00
1 2019-01-01 01:00:00
2 2019-01-01 02:00:00
3 2019-01-01 03:00:00
4 2019-01-01 04:00:00
5 2019-01-01 05:00:00
6 2019-01-01 06:00:00
7 2019-01-01 07:00:00
8 2019-01-01 08:00:00
9 2019-01-01 09:00:00