DromiX
DromiX

Reputation: 553

Round to nearest hour pd.Timedelta

I am calculating the time worked for the operators, there is a format table: operator, call accepted like this Source data
In the cycle I subtract 2-1, 3-2, etc and summarize in variable Time.

         if CurrentOperator == list.iloc[i][0]:
             DiffTime = list.iloc[i][1] - list.iloc[i-1][1]
             if DiffTime > pd.Timedelta('46 minutes'):
                DiffTime = pd.Timedelta('0 minutes')
             Time += DiffTime
         else:

Result
Result

How can I round time to nearest hour? If variable Time have type timedeltas.Timedelta

Upvotes: 0

Views: 414

Answers (1)

Nicolas Gervais
Nicolas Gervais

Reputation: 36624

You can use pandas.Timedelta.round(self, freq)

from pandas import Timedelta

time = Timedelta('09:55:49') 
print(time)
Out[5]: Timedelta('0 days 09:55:49')
Timedelta.round(time, 'h')
Out[6]: Timedelta('0 days 10:00:00')

Upvotes: 2

Related Questions