0

I have some data in a pandas df like:

time delay 2017/06/14 10:02:10.144927 PM 15 

The time is obviously a timestamp and in the delay is in seconds. df.dtypes are datetime64[ns] and int respectively. I want to add a column that has the timestamp of the original time plus the seconds passed, e.g.:

time delay end_time 2017/06/14 10:02:10.144927 PM 15 2017/06/14 10:02:25.144927 PM 

I'm trying to do

df["end_time"] = df.time.add(datetime.timedelta(seconds = df.time)) 

However I get an error TypeError: unsupported type for timedelta seconds component: Series. How would I do this? Do I need a lambda?

1
  • df.time is a series of numbers. I think you wanted this to be element wise right? Commented May 28, 2019 at 12:55

1 Answer 1

3

Use to_timedelta for convert integer column to timedeltas:

df["end_time"] = pd.to_datetime(df['time']).add(pd.to_timedelta(df.delay, unit='s')) print (df) time delay end_time 0 2017/06/14 10:02:10.144927 PM 15 2017-06-14 22:02:25.144927 

If need same format in output add Series.dt.strftime:

df["end_time"] = (pd.to_datetime(df['time']) .add(pd.to_timedelta(df.delay, unit='s')) .dt.strftime('%Y/%m/%d %H:%M:%S.%f %p')) print (df) time delay end_time 0 2017/06/14 10:02:10.144927 PM 15 2017/06/14 22:02:25.144927 PM 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.