Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH: Enhanced Parsing in eval Method for Special Characters #56491

Open
1 of 3 tasks
OyiboRivers opened this issue Dec 13, 2023 · 3 comments · May be fixed by #59151
Open
1 of 3 tasks

ENH: Enhanced Parsing in eval Method for Special Characters #56491

OyiboRivers opened this issue Dec 13, 2023 · 3 comments · May be fixed by #59151
Assignees
Labels

Comments

@OyiboRivers
Copy link

OyiboRivers commented Dec 13, 2023

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

@OyiboRivers OyiboRivers added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 13, 2023
@rhshadrach
Copy link
Member

Thanks for the report; +1 on making this consistent.

@rhshadrach rhshadrach added expressions pd.eval, query and removed Needs Triage Issue that has not been reviewed by a pandas team member labels Dec 14, 2023
@parthi-siva
Copy link
Contributor

take

@PedroVerardo
Copy link

take

@PedroVerardo PedroVerardo linked a pull request Jun 30, 2024 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants