Open Zeroto521 opened 2 years ago
take
This looks like a numexpr
issue as this works without it:
In [1]: import pandas as pd
In [2]: df = pd.DataFrame({'A': ['a', 'b'], 'B': ['1', '2']})
In [3]: df.eval("A + B", engine="python")
Out[20]:
0 a1
1 b2
dtype: object
In [4]: df2 = df.convert_dtypes()
In [5]: df2.eval("A + B", engine="python")
Out[22]:
0 a1
1 b2
dtype: string
Your example also works verbatim, if numexpr isn't installed.
Looks like an issue in pandas/core/computation/ops.py:105
judging by the traceback.
I have the same problem.
The problem appears to be with the Op.has_invalid_return_type
method. If one changes line 241 from:
obj_dtype_set = frozenset([np.dtype("object")])
To:
obj_dtype_set = frozenset([np.dtype("object"), str])
Then the problem appears fixed for me.
Pandas version checks
[X] I have checked that this issue has not already been reported.
[X] 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
`df.eval('a + "a"')`` error messages
```python --------------------------------------------------------------------------- TypeError Traceback (most recent call last)`df.convert_dtypes().eval('a + "a"')` error messages
```python --------------------------------------------------------------------------- TypeError Traceback (most recent call last)Issue Description
df.eval
can't concatenate string column and string via+
operator likedf.eval("string_column + 'a string'")
I also tried some other cases, but they also failed.
df.astype(str).eval("a + 'a'")
faildf.eval("a + 'a'", engine="python")
failExpected Behavior
Installed Versions
While run
pd.show_versions()
I noted I don't have 'numexpr'. So after finishing installing 'numexpr', above cases still couldn't work.