Pandas: extract hour from timedelta
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
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