T. Payne
T. Payne

Reputation: 79

Find "True" in dataframe and label X values before the True

I created a column in a dataframe that starts as np.nan and then returns True every time a certain date condition is met. Next I want to re-scan the column for the True values and label True for every X values (varies) above the previously labeled True as well. There must be a simple implementation of .loc but it is escaping me. One that combines both would be ideal.

index = pd.date_range('20170101',periods=10)    

main_df = pd.DataFrame(np.random.randn(10,1),index=index)
main_df['EXPIRY'] = np.nan

dates = {datetime(2017, 1, 5), datetime(2017, 1, 9)}
tag_days = 2    

#update. this works now   
for x in dates:
    main_df.loc[x-pd.Timedelta(str(tag_days)+'d'):x , ['EXPIRY']] = 'TRUE'

Upvotes: 2

Views: 70

Answers (1)

Scott Boston
Scott Boston

Reputation: 153460

Let's try:

pd.concat([pd.DataFrame({'EXPIRY':'TRUE'},index=pd.date_range(pd.to_datetime(i) - pd.DateOffset(days=tag_days),pd.to_datetime(i))) for i in date]).combine_first(main_df)

Output:

           EXPIRY         0
2017-01-01    NaN  2.003359
2017-01-02   TRUE -0.029419
2017-01-03   TRUE -1.582940
2017-01-04   TRUE -0.026711
2017-01-05   TRUE  1.571670
2017-01-06   TRUE -0.394502
2017-01-07   TRUE  1.434229
2017-01-08   TRUE -0.092047
2017-01-09   TRUE -0.520282
2017-01-10    NaN -1.698000

Upvotes: 1

Related Questions