Description
Feature Type
-
Adding new functionality to pandas
-
Changing existing functionality in pandas
-
Removing existing functionality in pandas
Problem Description
Hey Pandas team,
Currently, the eval
method treats special characters in column names different than the query
method.
The query
method allows for the use of backticks to enclose column names with special characters in contrast to the eval
method. This inconsistency can be confusing for users.
The proposal is to enhance the parsing capabilities of the eval
method to recognize and handle backticks in a manner similar to the query
method. This modification would provide users with more flexibility when working with DataFrame expressions, especially in cases where column names contain special characters.
Here is an example:
import pandas as pd
df = pd.DataFrame({'my.col': [1, 2, 3]})
qry = df.query('`my.col` > 2')
evl = df.eval('`my.col` = 0')
print('Original:')
print(df)
print('\nquery: `my.col` > 2')
print(qry)
print('\neval: `my.col` = 0')
print(evl)
# Original:
# my.col
# 0 1
# 1 2
# 2 3
# query: `my.col` > 2
# my.col
# 2 3
# eval: `my.col` = 0
# my.col BACKTICK_QUOTED_STRING_my_DOT_col
# 0 1 0
# 1 2 0
# 2 3 0
# Current behavior in eval
# Creates a column with modified name 'BACKTICK_QUOTED_STRING_my_DOT_col'
Feature Description
# Proposed behavior in eval
# Directly modifies the 'my.col' column
# df.eval('`my.col` = 0')
# my.col
# 0 0
# 1 0
# 2 0
Alternative Solutions
Can eval
parsing be adapted to the way query
parses?
Additional Context
Python 3.11.5
Pandas 2.1.1