I am trying to use the pandas.DataFrame.query() function as follows:
expression_string = 'ColumnName<(-1000)'
output_dataframe = dataframe.query(expression_string)
The code works with positive numbers, but when negative numbers are passed to the string, as above, it returns the following error:
AttributeError: 'UnaryOp' object has no attribute 'value'
Any suggestions on how to use negative numbers in DataFrame query() expressions? Thank you!!
The query() method allows you to query the DataFrame. The query() method takes a query expression as a string parameter, which has to evaluate to either True of False. It returns the DataFrame where the result is True according to the query expression.
This can be done by using abs function. For example, if we have a data frame df with many columns and each of them having some negative values then those values can be converted to positive values by just using abs(df).
Pandas query syntax Assuming you have a DataFrame, you need to call . query() using “dot syntax”. Basically, type the name of the DataFrame you want to subset, then type a “dot”, and then type the name of the method …. query() .
I can reproduce this error on pandas
v0.20.3 with specific data types; for example, np.float32
. The workaround is to cast explicitly as float
.
This is a known bug: DataFrame.eval errors with AttributeError: 'UnaryOp'
df = pd.DataFrame({'A': [-3.0, -2.5, -1.5, 3.0]}, dtype=np.float32)
x = 'A>(-1)'
# AttributeError:
res = df.query(x)
# Success:
df['A'] = df['A'].astype(float)
res = df.query(x)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With