Reputation: 31
I have the following dataframe column:
I need to convert object string data from the csv column into total seconds.
Example: 10m -> 600s
I tried this code:
df.duration = str(datetime.timedelta(df['duration']))
But the following error is displayed
TypeError: unsupported type for timedelta days component: Series
Upvotes: 3
Views: 3307
Reputation: 62403
'duration'
to seconds is to:
'duration'
to a timedeltapd.Timedelta(seconds=1)
.dt.seconds
timedelta
and why the .total_seconds
method is a total accident.import pandas as pd
# test data
df = pd.DataFrame({'duration': ['10d 15h 23m', '10d 18h 13m']})
# convert duration to a timedelta
df.duration = pd.to_timedelta(df.duration)
# calculate total_seconds
df['total_sec'] = df.duration / pd.Timedelta(seconds=1)
# get seconds for just hours, minutes, seconds
df['sec_without_days'] = df.duration.dt.seconds
# display(df)
duration total_sec sec_without_days
0 10 days 15:23:00 919380.0 55380
1 10 days 18:13:00 929580.0 65580
Upvotes: 3
Reputation: 455
You can use the pandas.to_timedelta()
to convert your strings to timedelta objects and then apply a lambda function to get the total seconds in the duration:
import pandas as pd
df['duration'] = pd.to_timedelta(df['duration']).apply(lambda x: x.total_seconds())
Upvotes: 2