Skip to content

ENB: support f-strings in pd.eval #52310

Open
@dss010101

Description

@dss010101

Pandas version checks

  • I have checked that this issue has not already been reported.

  • I have confirmed this bug exists on the latest version of pandas.

  • I have confirmed this bug exists on the main branch of pandas.

Reproducible Example

import pandas as pd, numpy as np
import timeit
class Data:
    def __init__(self, data, tag = 'xxx') -> None:
        self.data = data
        self.tag1 =  'xxx'
        
df = pd.DataFrame(np.random.randint(0,10,100).astype(str).reshape(50,2),columns=list('AB'))
d = Data(data=df)

eval("data.A + f':{tag1}'", d.__dict__)

pd.eval("data.A + f':{tag1}'", resolvers=(d.__dict__,))

Issue Description

In the above, eval works fine. However pd.eval throws the following exception

AttributeError: 'PandasExprVisitor' object has no attribute 'visit_JoinedStr'

Expected Behavior

If eval is able to evaluate this correctly, then pandas.eval should i feel.

For context, the reason for code like this, is that f strings dont seem to work with pandas series. so i cant do something like:

Installed Versions

Replace this line with the output of pd.show_versions()

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions