Skip to content

Commit 6ab128c

Browse files
committed
Fix pandas-dev#60494: query doesn't work on DataFrame integer column names
1 parent 40a8180 commit 6ab128c

File tree

3 files changed

+12
-0
lines changed

3 files changed

+12
-0
lines changed

doc/source/whatsnew/v3.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -826,6 +826,7 @@ Other
826826
- Bug in :meth:`DataFrame.eval` and :meth:`DataFrame.query` which did not allow to use ``tan`` function. (:issue:`55091`)
827827
- Bug in :meth:`DataFrame.query` where using duplicate column names led to a ``TypeError``. (:issue:`59950`)
828828
- Bug in :meth:`DataFrame.query` which raised an exception or produced incorrect results when expressions contained backtick-quoted column names containing the hash character ``#``, backticks, or characters that fall outside the ASCII range (U+0001..U+007F). (:issue:`59285`) (:issue:`49633`)
829+
- Bug in :meth:`DataFrame.query` which raised an exception when querying integer column names using backticks. (:issue:`60494`)
829830
- Bug in :meth:`DataFrame.shift` where passing a ``freq`` on a DataFrame with no columns did not shift the index correctly. (:issue:`60102`)
830831
- Bug in :meth:`DataFrame.sort_index` when passing ``axis="columns"`` and ``ignore_index=True`` and ``ascending=False`` not returning a :class:`RangeIndex` columns (:issue:`57293`)
831832
- Bug in :meth:`DataFrame.transform` that was returning the wrong order unless the index was monotonically increasing. (:issue:`57069`)

pandas/core/frame.py

+6
Original file line numberDiff line numberDiff line change
@@ -4790,6 +4790,12 @@ def eval(self, expr: str, *, inplace: bool = False, **kwargs) -> Any | None:
47904790
inplace = validate_bool_kwarg(inplace, "inplace")
47914791
kwargs["level"] = kwargs.pop("level", 0) + 1
47924792
index_resolvers = self._get_index_resolvers()
4793+
4794+
if any(isinstance(col, int) for col in self.columns):
4795+
self = self.rename(
4796+
columns={col: f"{col}" for col in self.columns if isinstance(col, int)}
4797+
)
4798+
47934799
column_resolvers = self._get_cleaned_column_resolvers()
47944800
resolvers = column_resolvers, index_resolvers
47954801
if "target" not in kwargs:

pandas/tests/frame/test_query_eval.py

+5
Original file line numberDiff line numberDiff line change
@@ -1345,6 +1345,11 @@ def test_start_with_spaces(self, df):
13451345
expect = df[" A"] + df[" "]
13461346
tm.assert_series_equal(res, expect)
13471347

1348+
def test_ints(self, df):
1349+
res = df.query("`1` == 7")
1350+
expect = df[df[1] == 7]
1351+
tm.assert_frame_equal(res, expect)
1352+
13481353
def test_lots_of_operators_string(self, df):
13491354
res = df.query("` &^ :!€$?(} > <++*'' ` > 4")
13501355
expect = df[df[" &^ :!€$?(} > <++*'' "] > 4]

0 commit comments

Comments
 (0)