I donβt think there is a built-in pandas or numpy method / function for this.
However, I would prefer to use a python generator:
def repeats(lst): i_0 = None n = -1
Then you can put this generator in a numpy array :
import numpy as np df['rep'] = np.array(list(repeats(df['time'])))
Count the repetitions:
from collections import Counter count = Counter(df['time']) df['count'] = df['time'].apply(lambda x: count[x])
and do the calculation (this is the most expensive part of the calculation):
df['time2'] = df.apply(lambda row: (row['time'] + datetime.timedelta(0, 1)
Note. To remove calculation columns, use del df['rep'] and del df['count'] .
.
One βbuilt-inβ way to do this can be done with shift twice, but I think it will be a little messier ...
source share