Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas: extract hour from timedelta

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'

like image 898
FaCoffee Avatar asked Aug 30 '18 09:08

FaCoffee


2 Answers

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
like image 72
jezrael Avatar answered Sep 28 '22 06:09

jezrael


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

like image 31
Wtower Avatar answered Sep 28 '22 04:09

Wtower