Hernan
Hernan

Reputation: 63

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

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.

Upvotes: 1

Views: 551

Answers (2)

user17242583
user17242583

Reputation:

Use .dt.total_seconds() instead of .seconds:

df['time'] = df['time'].dt.total_seconds() / 60

Upvotes: 1

Corralien
Corralien

Reputation: 120559

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')]})

Upvotes: 1

Related Questions