Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas, insert datetime values that increase one hour for each row

enter image description here

I made predictions with an Arima model that predict the next 168 hours (one week) of cars on the road. I also want to add a column called "datetime" that starts with 00:00 01-01-2021 and increases with one hour for each row.

Is there an intelligent way of doing this?

like image 820
Miles Bennet Dyson Avatar asked Dec 01 '25 06:12

Miles Bennet Dyson


1 Answers

You can do:

x=pd.to_datetime('2021-01-01 00:00')
y=pd.to_datetime('2021-01-07 23:59')
pd.Series(pd.date_range(x,y,freq='H'))

output:

pd.Series(pd.date_range(x,y,freq='H'))
Out[153]: 
0     2021-01-01 00:00:00
1     2021-01-01 01:00:00
2     2021-01-01 02:00:00
3     2021-01-01 03:00:00
4     2021-01-01 04:00:00
       
163   2021-01-07 19:00:00
164   2021-01-07 20:00:00
165   2021-01-07 21:00:00
166   2021-01-07 22:00:00
167   2021-01-07 23:00:00
Length: 168, dtype: datetime64[ns]
like image 148
Suhas Mucherla Avatar answered Dec 03 '25 18:12

Suhas Mucherla



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!