diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 6763c3043b102..16474dd83a1f5 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -234,6 +234,8 @@ Indexing - Bug in indexing on a :class:`MultiIndex` failing to drop scalar levels when the indexer is a tuple containing a datetime-like string (:issue:`42476`) - Bug in :meth:`DataFrame.sort_values` and :meth:`Series.sort_values` when passing an ascending value, failed to raise or incorrectly raising ``ValueError`` (:issue:`41634`) - Bug in updating values of :class:`pandas.Series` using boolean index, created by using :meth:`pandas.DataFrame.pop` (:issue:`42530`) +- Bug in :meth:`DataFrame.query` did not handle the degree sign in a backticked column name, such as \`Temp(°C)\`, used in an expression to query a dataframe (:issue:`42826`) +- Missing ^^^^^^^ diff --git a/pandas/core/computation/parsing.py b/pandas/core/computation/parsing.py index 5e000116d19f2..89d1f2133f77a 100644 --- a/pandas/core/computation/parsing.py +++ b/pandas/core/computation/parsing.py @@ -49,6 +49,7 @@ def create_valid_python_identifier(name: str) -> str: "!": "_EXCLAMATIONMARK_", "$": "_DOLLARSIGN_", "€": "_EUROSIGN_", + "°": "_DEGREESIGN_", # Including quotes works, but there are exceptions. "'": "_SINGLEQUOTE_", '"': "_DOUBLEQUOTE_", diff --git a/pandas/tests/computation/test_eval.py b/pandas/tests/computation/test_eval.py index f27112dbd3956..99c3cac9ba976 100644 --- a/pandas/tests/computation/test_eval.py +++ b/pandas/tests/computation/test_eval.py @@ -2035,6 +2035,16 @@ def test_truediv_deprecated(engine, parser): assert match in str(m[0].message) +@pytest.mark.parametrize("column", ["Temp(°C)", "Capacitance(μF)"]) +def test_query_token(engine, column): + # See: https://github.com/pandas-dev/pandas/pull/42826 + df = DataFrame(np.random.randn(5, 2), columns=[column, "b"]) + expected = df[df[column] > 5] + query_string = f"`{column}` > 5" + result = df.query(query_string, engine=engine) + tm.assert_frame_equal(result, expected) + + def test_negate_lt_eq_le(engine, parser): df = DataFrame([[0, 10], [1, 20]], columns=["cat", "count"]) expected = df[~(df.cat > 0)]