Skip to content

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

Open
@rwjmiller

Description

@rwjmiller

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions