Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot query dataframe using str.slice with negative indexes #49905

Open
rwjmiller opened this issue Nov 25, 2022 · 1 comment
Open

Cannot query dataframe using str.slice with negative indexes #49905

rwjmiller opened this issue Nov 25, 2022 · 1 comment
Labels
expressions pd.eval, query

Comments

@rwjmiller
Copy link

rwjmiller commented Nov 25, 2022

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:

df = pd.DataFrame({'a': ['example']})

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.

@jbrockmendel jbrockmendel added the expressions pd.eval, query label Jan 12, 2023
@joaoe
Copy link

joaoe commented Apr 9, 2024

Hi.
Came here to report the same thing

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
expressions pd.eval, query
Projects
None yet
Development

No branches or pull requests

3 participants