Skip to content

DOC: DataFrame.query() are contradictory on whether Python keywords can used as identifiers #35989

Closed
@jpeacock29

Description

@jpeacock29

Location of the documentation

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.query.html#pandas.DataFrame.query

Documentation problem

The expr parameter is described as follows:

You can refer to column names that contain spaces or operators by surrounding them in backticks. This way you can also escape names that start with a digit, or those that are a Python keyword. Basically when it is not valid Python identifier. See notes down for more details.

However, the notes that follow indicate:

Python keywords may not be used as identifiers.

So the first part suggests Python keywords can be used, while the second part indicates they cannot. Testing suggests they cannot currently be used.

Suggested fix for documentation

Change the expr description to read:

You can refer to column names that are not valid Python variable names by surrounding them in backticks. Thus column names containing spaces or punctuation (besides underscores) or starting with digits must be surrounded by backticks. (For example, a column named "Area (cm^2)" would be referenced as Area (cm^2).) Column names which are Python keywords (like "list", "for", "import", etc) cannot be used.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions