i have a Pandas dataframe like this:
Date Hour Actual
2018-06-01 0 0.000000
2018-06-01 1 0.012000
2018-06-01 2 0.065000
2018-06-01 3 0.560000
...
I want to convert these Hour integer indexes and add to date so that it is a Pandas' datetime object. The result should be like this:
Date Actual
2018-06-01 00:00:00 0.000000
2018-06-01 01:00:00 0.012000
2018-06-01 02:00:00 0.065000
2018-06-01 03:00:00 0.560000
...
What would be an efficient way to do that? Does Panda provide functionality for converting integer indexes into datetime objects?
Use to_datetime with to_timedelta and pop for extract column time:
df['Date'] = pd.to_datetime(df['Date']) + pd.to_timedelta(df.pop('Hour'), unit='H')
print (df)
Date Actual
0 2018-06-01 00:00:00 0.000
1 2018-06-01 01:00:00 0.012
2 2018-06-01 02:00:00 0.065
3 2018-06-01 03:00:00 0.560
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With