Skip to content

Commit 29a74e9

Browse files
fdionfeefladder
authored andcommitted
BUG: column names with degree sign make query fail (pandas-dev#42826)
1 parent 480d5bd commit 29a74e9

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

doc/source/whatsnew/v1.4.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,8 @@ Indexing
234234
- 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`)
235235
- 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`)
236236
- Bug in updating values of :class:`pandas.Series` using boolean index, created by using :meth:`pandas.DataFrame.pop` (:issue:`42530`)
237+
- 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`)
238+
-
237239

238240
Missing
239241
^^^^^^^

pandas/core/computation/parsing.py

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ def create_valid_python_identifier(name: str) -> str:
4949
"!": "_EXCLAMATIONMARK_",
5050
"$": "_DOLLARSIGN_",
5151
"€": "_EUROSIGN_",
52+
"°": "_DEGREESIGN_",
5253
# Including quotes works, but there are exceptions.
5354
"'": "_SINGLEQUOTE_",
5455
'"': "_DOUBLEQUOTE_",

pandas/tests/computation/test_eval.py

+10
Original file line numberDiff line numberDiff line change
@@ -2035,6 +2035,16 @@ def test_truediv_deprecated(engine, parser):
20352035
assert match in str(m[0].message)
20362036

20372037

2038+
@pytest.mark.parametrize("column", ["Temp(°C)", "Capacitance(μF)"])
2039+
def test_query_token(engine, column):
2040+
# See: https://github.com/pandas-dev/pandas/pull/42826
2041+
df = DataFrame(np.random.randn(5, 2), columns=[column, "b"])
2042+
expected = df[df[column] > 5]
2043+
query_string = f"`{column}` > 5"
2044+
result = df.query(query_string, engine=engine)
2045+
tm.assert_frame_equal(result, expected)
2046+
2047+
20382048
def test_negate_lt_eq_le(engine, parser):
20392049
df = DataFrame([[0, 10], [1, 20]], columns=["cat", "count"])
20402050
expected = df[~(df.cat > 0)]

0 commit comments

Comments
 (0)