Home > OS >  How to convert all Series values of a Dataframe from timedelta to minutes
How to convert all Series values of a Dataframe from timedelta to minutes

Time:02-11

I have this dataframe where every value's type is timedelta:

Pandas dataframe

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
  • Related