I have this dataframe where every value's type is timedelta:
And I want to transform all values to minutes, but when coding:
df['time'] = df['time'].seconds/60
I get the following error:
AttributeError: 'Series' object has no attribute 'seconds'
How can I transform every value of the column in a simple way?
Thanks.
CodePudding user response:
When you have a DatetimeIndex
series, you can access to a special accessor dt
which give you some attributes and methods:
df['min'] = df['time'].dt.total_seconds().div(60)
print(df)
# Output
time min
0 0 days 00:53:00 53.0
1 0 days 00:45:00 45.0
Setup:
df = pd.DataFrame({'time': [pd.Timedelta('0 days 00:53:00'),
pd.Timedelta('0 days 00:45:00')]})
CodePudding user response:
Use .dt.total_seconds()
instead of .seconds
:
df['time'] = df['time'].dt.total_seconds() / 60