Quantopian's community platform is shutting down. Please read this post for more information and download your code.
Back to Community
Replacing DataFrame data (Please Help!)

Please help, i'm able to create a DataFrame with context.stocks as the index, however, I'm trying to replace the data when i meet certain conditions; When I was running the system, the system didn't send me error; However, the data within the DataFrame still has not change at all.

def initialize(context):  
                context.stocks = [sid(42950), sid(47208), sid(46631), sid(3212), sid(8554)]  
                context.stop_loss_df = pd.DataFrame(index=context.stocks, columns=['long_short','current_price', 'exec_price','stop_loss'])

def handle_data(context, data):  
                context.stop_loss_df.replace({'long_short':{stock:'long'}})  
                context.stop_loss_df.replace({'current_price':{stock : data.current(stock,'price')}})  
                context.stop_loss_df.replace({'exec_price':{stock : data.current(stock,'price')}})  
                context.stop_loss_df.replace({'stop_loss':{stock : data.current(stock,'price') * 0.98}})  
                log.info(context.stop_loss_df)  
1 response

I found a way to replace, which is to use df.loc[row_indexer, column_indexer] function. Please see below. Hope this contributes

context.stop_loss_df.loc[stock, 'long_short'] = 'long'  
                context.stop_loss_df.loc[stock, 'current_price'] = current_price  
                context.stop_loss_df.loc[stock, 'exec_price'] = current_price  
                context.stop_loss_df.loc[stock, 'stop_loss'] = current_price*0.98