Reputation: 31
I tried this code but isn't doing what I wanted it to do. I'm try to get events to work on a pandas dataframe. If the Expiration Date equals the index date, then I want a value of 0. If the prediction column shows a 1 then I would like to get a 1 value. I would like it to check previous row if a 1 was already trigger that it stays 1 until the Expiration Date triggers a 0.
df['Forecast'] = np.select([df.ExpirationDate == df.index,
df.Predictions == 1,
df.Forecast.shift(1).eq(1)], [0, 1, 1])
I tried shift but it doesn't seem to work properly as it only does it once.
Open High ... ExpirationDate Predictions
Date ...
2017-09-18 249.610001 250.119995 ... 2017-10-20 0
2017-09-19 250.000000 250.070007 ... 2017-10-20 0
2017-09-20 250.070007 250.190002 ... 2017-10-20 0
2017-09-21 249.880005 249.979996 ... 2017-10-20 0
2017-09-22 249.050003 249.630005 ... 2017-10-20 0
Expected output
Predictions Forecast
Date
2017-09-18 0 0
2017-09-19 0 0
2017-09-20 0 0
2017-09-21 0 0
2017-09-22 0 0
2017-09-25 0 0
2017-09-26 0 0
2017-09-27 0 0
2017-09-28 0 0
2017-09-29 0 0
2017-10-02 0 0
2017-10-03 0 0
2017-10-04 0 0
2017-10-05 1 1
2017-10-06 0 1
2017-10-09 0 1
2017-10-10 0 1
2017-10-11 0 1
2017-10-12 0 1
2017-10-13 0 1
2017-10-16 0 1
2017-10-17 0 1
2017-10-18 0 1
2017-10-19 0 1
2017-10-20 0 0
So, Forecast stays 1 after a 1 in predictions is present. Forecast would turn back to 0 when the expiration is present.
Upvotes: 3
Views: 292
Reputation: 30920
We can also use Series.cumsum
+ Series.clip
:
df['Forecast']=(
df['Predictions'].cumsum()
.clip(0,1)
.where(df.index < df.ExpirationDate)
.fillna(0, downcast='infer')
)
print(df)
Output
Predictions ExpirationDate Forecast
Date
2017-09-18 0 2017-10-20 0
2017-09-19 0 2017-10-20 0
2017-09-20 0 2017-10-20 0
2017-09-21 0 2017-10-20 0
2017-09-22 0 2017-10-20 0
2017-09-25 0 2017-10-20 0
2017-09-26 0 2017-10-20 0
2017-09-27 0 2017-10-20 0
2017-09-28 0 2017-10-20 0
2017-09-29 0 2017-10-20 0
2017-10-02 0 2017-10-20 0
2017-10-03 0 2017-10-20 0
2017-10-04 0 2017-10-20 0
2017-10-05 1 2017-10-20 1
2017-10-06 0 2017-10-20 1
2017-10-09 0 2017-10-20 1
2017-10-10 0 2017-10-20 1
2017-10-11 0 2017-10-20 1
2017-10-12 0 2017-10-20 1
2017-10-13 0 2017-10-20 1
2017-10-16 0 2017-10-20 1
2017-10-17 0 2017-10-20 1
2017-10-18 0 2017-10-20 1
2017-10-19 0 2017-10-20 1
2017-10-20 0 2017-10-20 0
Upvotes: 3
Reputation: 59549
We can use Series.where
to mask, allowing for forward filling with fillna
. Another where
sets the values only prior to the expiration Date for each row and we set the remaining values back to 0.
df['Forecast'] = (df['Predictions'].where(df.Predictions.eq(1))
.ffill()
.where(df.index < df.ExpirationDate)
.fillna(0, downcast='infer'))
Predictions ExpirationDate Forecast
Date
2017-09-18 0 2017-10-20 0
2017-09-19 0 2017-10-20 0
2017-09-20 0 2017-10-20 0
2017-09-21 0 2017-10-20 0
2017-09-22 0 2017-10-20 0
2017-09-25 0 2017-10-20 0
2017-09-26 0 2017-10-20 0
2017-09-27 0 2017-10-20 0
2017-09-28 0 2017-10-20 0
2017-09-29 0 2017-10-20 0
2017-10-02 0 2017-10-20 0
2017-10-03 0 2017-10-20 0
2017-10-04 0 2017-10-20 0
2017-10-05 1 2017-10-20 1
2017-10-06 0 2017-10-20 1
2017-10-09 0 2017-10-20 1
2017-10-10 0 2017-10-20 1
2017-10-11 0 2017-10-20 1
2017-10-12 0 2017-10-20 1
2017-10-13 0 2017-10-20 1
2017-10-16 0 2017-10-20 1
2017-10-17 0 2017-10-20 1
2017-10-18 0 2017-10-20 1
2017-10-19 0 2017-10-20 1
2017-10-20 0 2017-10-20 0
Upvotes: 2