Pandas: extract hour from timedelta

21.6k Views Asked by At

This answer explains how to convert integers to hourly timesteps in Pandas. I need to do the opposite.

My dataframe df1:

   A
0  02:00:00
1  01:00:00
2  02:00:00
3  03:00:00

My expected dataframe df1:

   A         B
0  02:00:00  2
1  01:00:00  1
2  02:00:00  2
3  03:00:00  3

What I am trying:

df1['B'] = df1['A'].astype(int)

This fails because: TypeError: cannot astype a timedelta from [timedelta64[ns]] to [int32]

What is the best way to do this?

EDIT

If I try df['B'] = df['A'].dt.hour, then I get: AttributeError: 'TimedeltaProperties' object has no attribute 'hour'

4

There are 4 best solutions below

0
On BEST ANSWER

Divide by np.timedelta64(1, 'h'):

df1['B'] = df1['A'] / np.timedelta64(1, 'h')
print (df1)
         A    B
0 02:00:00  2.0
1 01:00:00  1.0
2 02:00:00  2.0
3 03:00:00  3.0
0
On

Alternatively divide by pd.Timedelta(1, 'h'):

df1['B'] = df1['A'] / pd.Timedelta(1, 'h')

The result is float.

https://pandas.pydata.org/docs/reference/api/pandas.Timedelta.html

1
On

Both solutions - dt.components or np.timedelta64 - are useful. But the np.timedelta64 is (1) much faster than dt.components (good to know especially for large dataframes) (2, as @Sam Chats noted) also takes into account the difference in days.

For time comparison:

import pandas as pd
import numpy as np

dct = { 
      'date1': ['08:05:23', '18:07:20', '08:05:23'],
      'date2': ['09:15:24', '22:07:20', '08:54:01']
      }
df = pd.DataFrame(dct)
df['date1'] = pd.to_datetime(df['date1'], format='%H:%M:%S')
df['date2'] = pd.to_datetime(df['date2'], format='%H:%M:%S')
df['delta'] = df['date2']-df['date1']

%timeit df['np_h'] = (df['delta'] / np.timedelta64(1,'h')).astype(int)
%timeit df['td_h'] = df['delta'].dt.components['hours']

Output:
1000 loops, best of 3: 484 µs per loop
1000 loops, best of 3: 1.43 ms per loop
2
On

You can use dt.components and access the hours column:

In[7]:
df['B'] = df['A'].dt.components['hours']
df

Out[7]: 
         A  B
0 02:00:00  2
1 01:00:00  1
2 02:00:00  2
3 03:00:00  3

the timedelta components returns each component as a column:

In[8]:
df['A'].dt.components

Out[8]: 
   days  hours  minutes  seconds  milliseconds  microseconds  nanoseconds
0     0      2        0        0             0             0            0
1     0      1        0        0             0             0            0
2     0      2        0        0             0             0            0
3     0      3        0        0             0             0            0