'AttributeError: 'UnaryOp' object has no attribute 'evaluate' when using eval function in Python

for test_ind, case_data in test_df.iterrows():
      case_data = case_data.to_frame().T
      rule = "Ask_before>-0.4843681 & 0.5255821<=BidVol_before<=0.07581073 & Volume>0.1107559"
      print(case_data, "case_data")
      if case_data.eval(rule).all() == True:
          print("TRUE")

Here, when the rule contains negative values, this error will appear. Could you please help me to fix this. I need to check whether this rule applies to the instances in the data-frame. Ask_before, BidVol_before are the columns of the dataframe test_df. Could you please help me to fix this issue.



Solution 1:[1]

It isn't a problem in your code, it's a bug in pandas https://github.com/pandas-dev/pandas/issues/16363 It is fixed by now.

Solution 2:[2]

Seems like it's still open as of 2022 January https://github.com/pandas-dev/pandas/issues/16363 in my experience resolving negative index values solved iit

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Leonid Mednikov
Solution 2