You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I can run any of the following without any problems:
df[df['a'].str.slice(0,6).str.contains("e")] # Works
df.query('a.str.slice(0,6).str.contains("e")') # Works - same output as above
df[df['a'].str.slice(0,-1).str.contains("e")] # Works
But not this:
df.query('a.str.slice(0,-1).str.contains("e")') # Generates an exception!
I get quite a long error message, terminating with
AttributeError: 'UnaryOp' object has no attribute 'value'
Is this perhaps a bug or known issue with Pandas when using the query syntax? I am using Pandas 1.5.1 and python 3.9.7.
The text was updated successfully, but these errors were encountered:
import pandas as pd
pd.DataFrame({"a": ["bc"]}).query("a.str[-1:] == 'c'")
# AttributeError: 'UnaryOp' object has no attribute 'value'
# Correcting to 1
pd.DataFrame({"a": ["bc"]}).query("a.str[1:] == 'c'")
# ValueError: "slice" is not a supported function
Apparently, string slicing operations don't work with query().
This however works df.a.str[-1:] == "c" just fine.
As per this question on SO:
https://stackoverflow.com/questions/74509756/using-pd-query-with-str-slice-with-negative-indexes-and-str-contains
I have the following example dataframe:
I can run any of the following without any problems:
But not this:
I get quite a long error message, terminating with
Is this perhaps a bug or known issue with Pandas when using the query syntax? I am using Pandas 1.5.1 and python 3.9.7.
The text was updated successfully, but these errors were encountered: