From 24dd7bad26006f2d4f57e1061689fd4ee4cb735b Mon Sep 17 00:00:00 2001 From: Sam Ezebunandu Date: Sat, 12 Sep 2020 13:36:20 -0600 Subject: [PATCH] DOC: Fix DataFrame.query contradiction on use of Python keywords as identifiers --- pandas/core/frame.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/pandas/core/frame.py b/pandas/core/frame.py index b03593ad8afe1..b8b49156a0967 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -3238,11 +3238,12 @@ def query(self, expr, inplace=False, **kwargs): in the environment by prefixing them with an '@' character like ``@a + b``. - 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. + You can refer to column names that are not valid Python variable names + by surrounding them in backticks. Thus, column names containing spaces + or punctuations (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. For example, if one of your columns is called ``a a`` and you want to sum it with ``b``, your query should be ```a a` + b``.