Difficult date calculation in DataFrame in Pyton Pandas?

I have DataFrame like below:

rng = pd.date_range('2020-12-11', periods=5, freq='T')
df = pd.DataFrame({ 'Date': rng, 'status': ['active', 'active', 'finished', 'finished', 'active'] }) 

And I need to create 2 new columns in this DataFrame:

  1. New1 = amount of days from “Date” column until today for status ‘active’
  2. New2 = amount of days from “Date” column until today for status ‘finished’

Below sample result:

enter image description here


Use Series.rsub for subtract from right side with today by Timestamp and Timestamp.floor, convert timedeltas to days by Series.dt.days and assign new columns by condition in Series.where:

rng = pd.date_range('2020-12-01', periods=5, freq='D')
df = pd.DataFrame({ 'Date': rng, 
                    'status': ['active', 'active', 'finished', 'finished', 'active'] }) 

days = df['Date'].rsub(pd.Timestamp('now').floor('d')).dt.days

df['New1'] = days.where(df['status'].eq('active'))
df['New2'] = days.where(df['status'].eq('finished'))
print (df)
        Date    status  New1  New2
0 2020-12-01    active  13.0   NaN
1 2020-12-02    active  12.0   NaN
2 2020-12-03  finished   NaN  11.0
3 2020-12-04  finished   NaN  10.0
4 2020-12-05    active   9.0   NaN