Home > database >  Looping through dataframe
Looping through dataframe

Time:02-21

enter image description here

Hello all

This is a column in my dataframe. I need to add 30 seconds to each time and append new time. How to do it.

My solution should look like this

0 2022-01-28 15:43:45
1 2022-01-28 15:44:15
2 2022-01-28 15:44:45

CodePudding user response:

You could create a Series of '30s'es and find cumsum. Then add it back to the original Series:

s = pd.Series(['2022-01-28 15:43:45', '2022-01-28 15:43:45', '2022-01-28 15:43:45', '2022-01-28 15:53:46', '2022-01-28 15:53:46'], dtype='datetime64[ns]')
s = s   pd.Series(['30s']*len(s), dtype='timedelta64[ns]').cumsum().shift().fillna('0s')

Output:

0   2022-01-28 15:43:45
1   2022-01-28 15:44:15
2   2022-01-28 15:44:45
3   2022-01-28 15:55:16
4   2022-01-28 15:55:46
dtype: datetime64[ns]
  • Related