问题
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'
回答1:
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
回答2:
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
回答3:
Both solutions - dt.components or np.timedelta64 - are useful. Just the np.timedelta64 is much faster than dt.components (good to know especially for large dataframes):
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
And, as @EdChum noted, dt.components['hours']
returns just value of hours < 24, what is indeed not a problem in case of this question. But for full dates where delta > 24 hours dt.components['days']*24+dt.components['hours']
must be used (which doubles processing time).
来源:https://stackoverflow.com/questions/52093199/pandas-extract-hour-from-timedelta