0

I have this part of code that when I run I receive this error:

SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame

See the caveats in the documentation: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html#returning-a-view-versus-a-copy self._setitem_single_block(indexer, value, name)

   for d in rolling_days:
        std = df[f'rolling_{d}'].std()

        sh_list = (df[f'rolling_{d}'] - std * 0.5)

        sl_list = (df[f'rolling_{d}'] + std * 0.5)

        df[f'sh_ltc_{d}']=0

        df[f'sl_ltc_{d}']=0

        for i in range(len(df)-1):
            if str(sh_list[i+1])!='nan' and str(df[f'sh_ltc_{d}'].iloc[i]) !='nan':
                df[f'sh_ltc_{d}'].iloc[i+1]=sh_list[i+1]+df[f'sh_ltc_{d}'].iloc[i]
                if df[f'sh_ltc_{d}'].iloc[i+1]<0:
                    df[f'sh_ltc_{d}'].iloc[i+1]=0

            if str(sl_list[i+1])!='nan' and str(df[f'sl_ltc_{d}'].iloc[i]) !='nan':
                df[f'sl_ltc_{d}'].iloc[i+1]=sl_list[i+1]+df[f'sl_ltc_{d}'].iloc[i]
                if df[f'sl_ltc_{d}'].iloc[i+1]>0:
                    df[f'sl_ltc_{d}'].iloc[i+1]=0

The output is totally fine for me, but is there a way to avoid this error?

0 Answers0