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

BUG: eval fails for ExtensionArray #58748

Open
3 tasks done
mutricyl opened this issue May 17, 2024 · 2 comments · May be fixed by #58793
Open
3 tasks done

BUG: eval fails for ExtensionArray #58748

mutricyl opened this issue May 17, 2024 · 2 comments · May be fixed by #58793
Labels
Bug expressions pd.eval, query ExtensionArray Extending pandas with custom dtypes or arrays.

Comments

@mutricyl
Copy link
Contributor

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

df = pd.DataFrame({'a': pd.array([1, 2, 3]), 'b': pd.array([4, 5, 6])})

# this works as expected
print(df['a'] / df['b'])
"""
0    0.25
1     0.4
2     0.5
dtype: Float64
"""

# this is not working
print(df.eval('a / b'))
"""
TypeError: Cannot interpret 'Int64Dtype()' as a data type
"""

Issue Description

This issue is coming from pint-pandas#137. ExtensionArray is used for this project and df.eval inlcuding division (\) fails with TypeError.

I was able to able to reproduce the pint-pandas issue with pandas build in IntegerArray as shown in the above example

Traceback (most recent call last):
  File "c:\CALC\DEV\pint pandas debug\test.py", line 17, in <module>
    print(df.eval('a / b'))
          ^^^^^^^^^^^^^^^^
  File "C:\Users\laurent.mutricy\AppData\Local\miniconda3\Lib\site-packages\pandas\core\frame.py", line 4738, in eval
    return _eval(expr, inplace=inplace, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\laurent.mutricy\AppData\Local\miniconda3\Lib\site-packages\pandas\core\computation\eval.py", line 340, in eval
    parsed_expr = Expr(expr, engine=engine, parser=parser, env=env)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\laurent.mutricy\AppData\Local\miniconda3\Lib\site-packages\pandas\core\computation\expr.py", line 809, in __init__
    self.terms = self.parse()
                 ^^^^^^^^^^^^
  File "C:\Users\laurent.mutricy\AppData\Local\miniconda3\Lib\site-packages\pandas\core\computation\expr.py", line 828, in parse
    return self._visitor.visit(self.expr)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\laurent.mutricy\AppData\Local\miniconda3\Lib\site-packages\pandas\core\computation\expr.py", line 413, in visit
    return visitor(node, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\laurent.mutricy\AppData\Local\miniconda3\Lib\site-packages\pandas\core\computation\expr.py", line 419, in visit_Module
    return self.visit(expr, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\laurent.mutricy\AppData\Local\miniconda3\Lib\site-packages\pandas\core\computation\expr.py", line 413, in visit
    return visitor(node, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\laurent.mutricy\AppData\Local\miniconda3\Lib\site-packages\pandas\core\computation\expr.py", line 422, in visit_Expr
    return self.visit(node.value, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\laurent.mutricy\AppData\Local\miniconda3\Lib\site-packages\pandas\core\computation\expr.py", line 413, in visit
    return visitor(node, **kwargs)
           ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\laurent.mutricy\AppData\Local\miniconda3\Lib\site-packages\pandas\core\computation\expr.py", line 535, in visit_BinOp
    return self._maybe_evaluate_binop(op, op_class, left, right)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\laurent.mutricy\AppData\Local\miniconda3\Lib\site-packages\pandas\core\computation\expr.py", line 502, in _maybe_evaluate_binop
    res = op(lhs, rhs)
          ^^^^^^^^^^^^
  File "C:\Users\laurent.mutricy\AppData\Local\miniconda3\Lib\site-packages\pandas\core\computation\expr.py", line 538, in <lambda>
    return lambda lhs, rhs: Div(lhs, rhs)
                            ^^^^^^^^^^^^^
  File "C:\Users\laurent.mutricy\AppData\Local\miniconda3\Lib\site-packages\pandas\core\computation\ops.py", line 528, in __init__
    if not isnumeric(lhs.return_type) or not isnumeric(rhs.return_type):
           ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\laurent.mutricy\AppData\Local\miniconda3\Lib\site-packages\pandas\core\computation\ops.py", line 512, in isnumeric
    return issubclass(np.dtype(dtype).type, np.number)
                      ^^^^^^^^^^^^^^^
TypeError: Cannot interpret 'Int64Dtype()' as a data type

Looking at the traceback it seams that pandas\core\computation\ops.isnumeric() does not handle properly dtypes from ExtensionArray class (or subclass). It was suggested to consider _is_numeric class attribute.

def isnumeric(dtype) -> bool:
-    return issubclass(np.dtype(dtype).type, np.number)
+   return getattr(dtype, '_is_numeric', False) or issubclass(np.dtype(dtype).type, np.number)

probably linked to #21374

Expected Behavior

df.eval('a / b') should give the same result than df['a'] / df['b']

Installed Versions

INSTALLED VERSIONS

commit : 4fb94bb
python : 3.11.4.final.0
python-bits : 64
OS : Windows
OS-release : 10
Version : 10.0.19045
machine : AMD64
processor : Intel64 Family 6 Model 186 Stepping 2, GenuineIntel
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : fr_FR.cp1252

pandas : 3.0.0.dev0+983.g4fb94bb09a
numpy : 1.26.4
pytz : 2024.1
dateutil : 2.8.2
setuptools : 67.8.0
pip : 23.1.2
Cython : None
pytest : 8.2.0
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : None
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : None
IPython : None
pandas_datareader : None
adbc-driver-postgresql: None
adbc-driver-sqlite : None
bs4 : None
bottleneck : None
fastparquet : None
fsspec : None
gcsfs : None
matplotlib : 3.8.3
numba : None
numexpr : 2.10.0
odfpy : None
openpyxl : 3.1.2
pyarrow : 15.0.0
pyreadstat : None
python-calamine : None
pyxlsb : None
s3fs : None
scipy : 1.12.0
sqlalchemy : None
tables : None
tabulate : 0.9.0
xarray : 2024.2.0
xlrd : None
zstandard : 0.19.0
tzdata : 2023.3
qtpy : None
pyqt5 : None

@mutricyl mutricyl added Bug Needs Triage Issue that has not been reviewed by a pandas team member labels May 17, 2024
@mutricyl mutricyl changed the title BUG: eval fails BUG: eval fails for ExtensionArray May 17, 2024
@rhshadrach
Copy link
Member

Thanks for the report! Further investigations and PRs to fix are welcome.

@rhshadrach rhshadrach added ExtensionArray Extending pandas with custom dtypes or arrays. expressions pd.eval, query and removed Needs Triage Issue that has not been reviewed by a pandas team member labels May 19, 2024
SiddheshBangar added a commit to SiddheshBangar/pandas that referenced this issue May 19, 2024
@SiddheshBangar SiddheshBangar linked a pull request May 19, 2024 that will close this issue
5 tasks
@SiddheshBangar
Copy link
Contributor

Hey @mutricyl @rhshadrach , thank you for mentioning the issue properly and in detailed manner. I have looked into it and have issued a PR as well, do check if any issues in the PR let me know I will work on it, Thanks once again. Cheers

SiddheshBangar added a commit to SiddheshBangar/pandas that referenced this issue May 21, 2024
SiddheshBangar added a commit to SiddheshBangar/pandas that referenced this issue May 21, 2024
SiddheshBangar added a commit to SiddheshBangar/pandas that referenced this issue May 22, 2024
SiddheshBangar added a commit to SiddheshBangar/pandas that referenced this issue May 22, 2024
mutricyl pushed a commit to mutricyl/pandas that referenced this issue May 24, 2024
mutricyl pushed a commit to mutricyl/pandas that referenced this issue May 24, 2024
SiddheshBangar added a commit to SiddheshBangar/pandas that referenced this issue May 28, 2024
SiddheshBangar added a commit to SiddheshBangar/pandas that referenced this issue May 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug expressions pd.eval, query ExtensionArray Extending pandas with custom dtypes or arrays.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants