jam
jam

Reputation: 567

Adding two time columns together in pandas dataframe?

I have the following data:

 time_begin  DRTN_IN_SCND
 16:22:16           439
 16:29:37            53
 16:30:33            85

I would like to make a new column that adds time_begin and DRTN_IN_SCND (the duration in seconds) to create a new time.

I have tried:

df['new_time'] = df['time_begin'].apply(lambda x: (dt.datetime.combine(dt.datetime(1,1,1), x,) + dt.timedelta(seconds=df.DRTN_IN_SCND)).time())

This works if dt.timedelta(seconds=3) but does not work when I change to dt.timedelta(seconds=df.DRTN_IN_SCND). I get the following error.

TypeError: unsupported type for timedelta seconds component: Series

Does anyone know how to fix this or of another way to accomplish what I'm trying to do? Thanks!

Upvotes: 5

Views: 6415

Answers (2)

Deusdeorum
Deusdeorum

Reputation: 1426

You'll have to convert the DRTN_IN_SCND and time_begin to time deltas if you want to do properly calculations on the columns, pandas has to_timedelta which is pretty handy:

df['DRTN_IN_SCND'] = pd.to_timedelta(df['DRTN_IN_SCND'], unit='s')
df['time_begin'] = pd.to_timedelta(df['time_begin'])
df['new_time'] = df['time_begin'] + df['DRTN_IN_SCND']

This will give you the new column new_time:

   time_begin  DRTN_IN_SCND  new_time
0    16:22:16      00:07:19  16:29:35
1    16:29:37      00:00:53  16:30:30
2    16:30:33      00:01:25  16:31:58

EDIT:

I'd write this as a chain nowadays.

df = (df
    .assign(
        DRTN_IN_SCND = pd.to_timedelta(df['DRTN_IN_SCND']),
        time_begin = pd.to_timedelta(df['time_begin']),
        new_time = lambda df_: pd.to_timedelta(df_.time_begin + df_.DRTN_IN_SCND)
    )
)

Upvotes: 6

piRSquared
piRSquared

Reputation: 294218

Problem

You are using apply on df['new_time'] which is a series and in the lambda you are referring to df.DRTN_IN_SCND which is another series. So the error states that you are trying to add a time object to a series object and it doesn't know what to do.

Solution

Instead, use apply on the dataframe. In this context, the x's in the lambda function are series and you can access each component via ix. This does what you want.

df['new_time'] = df.apply(lambda x: x.ix['time_begin'] + dt.timedelta(seconds=x.ix['DRTN_IN_SCND']), axis=1)

Upvotes: 1

Related Questions