-1

enter image description here

So I have two columns of data in my dataframe. TimeDeltasDiffs and ActualTime. First row of the dataframe has a start time in the ActualTime column. I want to populate the ActualTime column incrementally based on a time delta value in the current row.

I've used apply and lambdas for other operations on this dataframe. e.g.

df_mrx['Log Timestamp'] = df_mrx['Log Timestamp'].apply(lambda x: '00:' + x)

But not sure how to use that based on previous values as I am iterating ...

Any help would be greatly appreciated.

roganjosh
  • 12,594
  • 4
  • 29
  • 46
Auburnate
  • 437
  • 1
  • 4
  • 11
  • Just to clarify, the value of the nth row in column ActualTime is the value of the nth - 1 row of ActualTime plus the value of the nth row of TimeDeltasDiffs. – Auburnate May 10 '18 at 15:45

1 Answers1

1

You do not necessarily need to use .apply(). Consider below approach. Given that below is the df

       ActualTime           TimeDeltasDiffs
0   2018-04-16 17:06:01      00:00:00
1        0                   00:00:01
2        0                   00:00:00
3        0                   00:00:02

You need to use .cumsum() on TimeDeltasDiffs

df['ActualTime'] = df.iloc[0]['ActualTime']
df['ActualTime'] = pd.to_datetime(df['ActualTime']) + pd.to_timedelta(df['TimeDeltasDiffs']).cumsum()

Output:

         ActualTime            TimeDeltasDiffs
0   2018-04-16 17:06:01           00:00:00
1   2018-04-16 17:06:02           00:00:01
2   2018-04-16 17:06:02           00:00:00
3   2018-04-16 17:06:04           00:00:02
harvpan
  • 8,571
  • 2
  • 18
  • 36