Fix the broken loop error - return self.abs() - bad operand type for abs(): 'str'

Viewed 19

I have a class that substitutes random values into formulas and counts tables for me. I call it by the Tkinter application with a while loop.

Here is what the part of the class function looks like, in which the loop is interrupted and its complete completion occurs after n number of iterations

        df = pd.read_csv('Datasets\\Poisk\\PlustMath.csv')
        df['value_shifted'] = df["Price"].shift(-1)
        df['Sequence_shifted'] = df["combined"].shift(-1)
        new_df = df.where(np.abs(df['combined']) == 2, ).dropna()
        new_df['long'] = new_df['value_shifted'] - new_df['Price']
        new_df['short'] = new_df['Price'] - new_df['value_shifted']
        new_df['calc'] = np.nan
        new_df['calc'][new_df['combined'] == 2] = new_df['long']
        new_df['calc'][new_df['combined'] == -2] = new_df['short']
        new_df = new_df.drop(columns=['long', 'short'])
        a = (new_df.loc[new_df['calc'] > 0 , 'calc'].sum())+(new_df.loc[new_df['calc'] < 0 , 'calc'].sum())
    

Every iteration I get a warning:

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
  new_df['calc'][new_df['combined'] == 2] = new_df['long']
c:\kodit\magnat\GenPoisk.py:107: 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
  new_df['calc'][new_df['combined'] == -2] = new_df['short']

My primary solution was to use:

warnings.filterwarnings("ignore")

This removed the warnings, but later I discovered an error

The error itself:

Traceback (most recent call last):
  File "C:\Program Files\Python310\lib\tkinter\__init__.py", line 1921, in __call__
    return self.func(*args)
  File "c:\kodit\magnat\FermEma.py", line 53, in claculate
    GenPoisk(self.csvfile1, 5, P1, P2, P3, P4, P5, P6, P7, P8)
  File "c:\kodit\magnat\GenPoisk.py", line 165, in GenPoisk
    Poisk(file, n, H, b, c, d, e, f, g, h)
  File "c:\kodit\magnat\GenPoisk.py", line 11, in __init__
    self.PlustMath(file, n )
  File "c:\kodit\magnat\GenPoisk.py", line 102, in PlustMath
    new_df = df.where(np.abs(df['combined']) == 2, ).dropna()
  File "C:\Users\neshn\AppData\Roaming\Python\Python310\site-packages\pandas\core\generic.py", line 2101, in __array_ufunc__
    return arraylike.array_ufunc(self, ufunc, method, *inputs, **kwargs)
  File "C:\Users\neshn\AppData\Roaming\Python\Python310\site-packages\pandas\core\arraylike.py", line 263, in array_ufunc
    result = maybe_dispatch_ufunc_to_dunder_op(self, ufunc, method, *inputs, **kwargs)
  File "pandas\_libs\ops_dispatch.pyx", line 105, in pandas._libs.ops_dispatch.maybe_dispatch_ufunc_to_dunder_op
  File "C:\Users\neshn\AppData\Roaming\Python\Python310\site-packages\pandas\core\generic.py", line 1652, in __abs__
    return self.abs()
  File "C:\Users\neshn\AppData\Roaming\Python\Python310\site-packages\pandas\core\generic.py", line 1647, in abs
    res_mgr = self._mgr.apply(np.abs)
  File "C:\Users\neshn\AppData\Roaming\Python\Python310\site-packages\pandas\core\internals\managers.py", line 302, in apply
    applied = b.apply(f, **kwargs)
  File "C:\Users\neshn\AppData\Roaming\Python\Python310\site-packages\pandas\core\internals\blocks.py", line 402, in apply
    result = func(self.values, **kwargs)
TypeError: bad operand type for abs(): 'str'

Can someone suggest how to fix it ?

0 Answers
Related