Reputation: 258
I have a dataframe with 3 columns: timedeltas (duration) of time slot, datetime of slot start and datetime informing when record was created. Timedeltas are all multipliers of 15 minutes:
Index duration slot_start creation_time
1. 15 minutes some datetime 1 some datetime 3
2. 45 minutes some datetime 2 some datetime 4
What I want to achieve is:
Index duration slot_start creation_time
1. 15 minutes some datetime 1 some datetime 3
2. 15 minutes some datetime 2 some datetime 4
3. 15 minutes some datetime 2 + 15 minutes some datetime 4
4. 15 minutes some datetime 2 + 30 minutes some datetime 4
Is there any tool for such operation? How to achieve it easily and time efficiently on very large dataframes?
Upvotes: 2
Views: 112
Reputation: 93151
Try this:
unit = pd.Timedelta(minutes=15)
s = pd.to_timedelta(df['duration']).div(unit) \
.apply(lambda n: unit * np.arange(n)) \
.rename('offset') \
.explode()
df = df.join(s)
df['slot_start'] = df['slot_start'] + df['offset']
Upvotes: 1