0

when I run this code below, I get the right result, apparently, but I also get the error in the title. I checked the other similar questions but I cannot relate them to my case

df_zinc['buy_sell'] = 0

index = 0

while index < df_zinc.shape[0]:
    if df_zinc['action'][index] == 1:
        if df_zinc['PX_LAST'][index]<df_zinc['ma'][index]:
            df_zinc.loc[index,'buy_sell'] = -1
        else:
            df_zinc.loc[index,'buy_sell'] = 1
    if df_zinc['action'][index] == -1:
            df_zinc['buy_sell'][index] = df_zinc['buy_sell'][index-avg_window]*-1 
    index=index+1
df_zinc.buy_sell.value_counts()
  • Could you explain `df_zinc`'s data structure ? It would help. – Valentino Mar 20 '19 at 19:28
  • Hi user11233775, please give a runnable example so that we can run on our side and tell you exactly what is going on. Check out: https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – Rich Andrews Mar 20 '19 at 19:52
  • 2
    Try replacing `df_zinc['buy_sell'][index] = df_zinc['buy_sell'][index-avg_window]*-1` with `df_zinc.loc[index, 'buy_sell'] = df_zinc['buy_sell'][index-avg_window]*-1`. As an aside, it should be possible to write this logic in a more readable manner without explicit looping; see, for example, https://pandas.pydata.org/pandas-docs/stable/user_guide/cookbook.html – Peter Leimbigler Mar 20 '19 at 19:55
  • Was hoping to get an mcve in the Q. But yeah what Peter says. Also more on chained assignment warnings and how to deal with them: https://stackoverflow.com/questions/20625582/how-to-deal-with-settingwithcopywarning-in-pandas – Rich Andrews Mar 20 '19 at 20:12

0 Answers0