how to do addition dataframe of different type of TImestamp columns in python

Question:

below is my dataframe and I want to do an addition operation which i have mentioned down.

a                            b
0 days 19:18:43.997000       4
0 days 19:18:44.039000       4

b is a result of df['time:timestamp'].dt.dayofweek.

when i do following code df['a'] + (df['b'] * 3600 * 24), it throws TypeError: Addition/subtraction of integers and integer-arrays with TimedeltaArray is no longer supported. Instead of adding/subtracting n, use n * obj.freq

type of a -: Timedelta(‘0 days 19:18:43.997000’)

type of b -: numpy.int64

can someone help me with this?

Asked By: Coder

||

Answers:

You need to convert b back to timedelta:

df['a'] + pd.to_timedelta(df['b'], unit='d')

Or

df['a'] + df['b'] * pd.Timedelta('1D')

Output:

0   4 days 19:18:43.997000
1   4 days 19:18:44.039000
dtype: timedelta64[ns]
Answered By: Quang Hoang
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.