Jenny Linderud
Jenny Linderud

Reputation: 25

How do I convert this TimeDelta column to total minutes in pandas Datafram

See column here for visualisation

I have tried the following without luck:

df['TimedSpentInZone'] = df.TimedSpentInZone.astype(int)

df['TimedSpentInZone'] = df['TimedSpentInZone'].dt.total_seconds() 

df['TimedSpentInZone'] = df['TimedSpentInZone'].dt.hours

(And also divided by 60 etc to get minutes. none of the above works.

Upvotes: 2

Views: 5365

Answers (2)

Tharaka Devinda
Tharaka Devinda

Reputation: 2022

Its been a long time, but for future readers, this is what I did;

From the looks of it this column in the question is already a Timedelta type. If thats your case, you don't need to convert it.

Just get

df['TimeSpentInZone'].dt.seconds / 60

to get the minutes.

Upvotes: 2

Quang Hoang
Quang Hoang

Reputation: 150815

You can, and should convert to Timedelta type:

pd.to_timedelta(df['TimedSpentInZone'])/pd.Timedelta('60s')

Upvotes: 5

Related Questions