DumbCoder21
DumbCoder21

Reputation: 113

Forward fill pandas based on column conditions with increment

I have the following dataframe. I would like to forward fill from the startTime till the endTime for each id I have.

id current    startTime  endTime
1  2015-05-10 2015-05-10 2015-05-12
2  2015-07-11 2015-07-11 2015-07-13
3  2015-10-01 2015-10-01 2015-10-03
4  2015-12-01 None       None

Here's my expected output:

id current    
1  2015-05-10
1  2015-05-11
1  2015-05-12 
2  2015-07-11 
2  2015-07-12 
2  2015-07-13 
3  2015-10-01 
3  2015-10-02 
3  2015-10-03
4  2015-12-01

Upvotes: 0

Views: 183

Answers (1)

Emma
Emma

Reputation: 9363

I use date_range and explode for this.

df['current'] = df.apply(lambda row: row['current'] if row['startTime'] is None else pd.date_range(row['startTime'], row['endTime'], freq='D'), axis=1) 

df = df.explode('current')

Upvotes: 1

Related Questions