I have a dataframe that looks like this, the last two columns I added to help me understand the time difference. I would like the time difference between the timestamp and forecastRan to be rounded up to the next hour and add it as a column to my dataframe. However, it just gives me the hours (not rounded up).
timeStamp forecastRan time_diff_in_hours time_diff_seconds
12 2016-11-23 23:00:00 2016-11-23 12:18:00 10.0 38520.0
13 2016-11-24 00:00:00 2016-11-23 12:18:00 11.0 42120.0
14 2016-11-24 01:00:00 2016-11-23 12:18:00 12.0 45720.0
These are ways I've gotten the difference
df['time_diff_in_hours'] = (df['timeStamp'] - df['forecastRan']).astype('timedelta64[h]')
df['time_diff']= (df['timeStamp'] - df['forecastRan']).dt.total_seconds()
However, the first one just gives me the hours. With the second one I tried dividing by 3600 to get the hours and then using math.ceil to round up, like this:
import datetime
import math
df['time_diff']= math.ceil(((df['timeStamp'] - df['forecastRan']).dt.total_seconds()/3600))
I get: TypeError: cannot convert the series to <class 'float'>. I think the problem happened when I introudced math.ceil, because when I just used (df['timeStamp'] - df['forecastModelRun']).dt.total_seconds()/3600, I didn't get any errors. Not sure where exactly to round up and how to.
I believe need np.ceil:
df['time_diff']= np.ceil(((df['timeStamp'] - df['forecastRan']).dt.total_seconds()/3600))
print (df)
timeStamp forecastRan time_diff_in_hours \
0 2016-11-23 23:00:00 2016-11-23 12:18:00 10.0
1 2016-11-24 00:00:00 2016-11-23 12:18:00 11.0
2 2016-11-24 01:00:00 2016-11-23 12:18:00 12.0
time_diff_seconds time_diff
0 38520.0 11.0
1 42120.0 12.0
2 45720.0 13.0
Or dt.ceil:
df['time_diff']= (df['timeStamp'] - df['forecastRan']).dt.ceil('h')
print (df)
timeStamp forecastRan time_diff_in_hours \
0 2016-11-23 23:00:00 2016-11-23 12:18:00 10.0
1 2016-11-24 00:00:00 2016-11-23 12:18:00 11.0
2 2016-11-24 01:00:00 2016-11-23 12:18:00 12.0
time_diff_seconds time_diff
0 38520.0 11:00:00
1 42120.0 12:00:00
2 45720.0 13:00:00
df['time_diff' ]= (df['timeStamp'] - df['forecastRan']).dt.ceil('h').astype('timedelta64[h]')
print (df)
timeStamp forecastRan time_diff_in_hours \
0 2016-11-23 23:00:00 2016-11-23 12:18:00 10.0
1 2016-11-24 00:00:00 2016-11-23 12:18:00 11.0
2 2016-11-24 01:00:00 2016-11-23 12:18:00 12.0
time_diff_seconds time_diff
0 38520.0 11.0
1 42120.0 12.0
2 45720.0 13.0
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