Skip to content

Pandas dataframe eval #24883

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

Closed
poeticcode01 opened this issue Jan 23, 2019 · 2 comments · Fixed by #35794
Closed

Pandas dataframe eval #24883

poeticcode01 opened this issue Jan 23, 2019 · 2 comments · Fixed by #35794
Labels
Bug expressions pd.eval, query Numeric Operations Arithmetic, Comparison, and Logical operations
Milestone

Comments

@poeticcode01
Copy link

Code Sample, a copy-pastable example if possible

# Your code here
Scenario 1:

df2=pd.DataFrame({'a1':[10,20]})

df2.eval("c=((a1>10) & True )")
Out[8]: 
   a1      c
0  10  False
1  20   True
Scenario 2:
df2=pd.DataFrame({'a1':['Y','N']})
df2.eval("c=((a1 == 'Y') & True )")
Traceback (most recent call last):

TypeError: issubclass() arg 1 must be a class




Problem description

[this should explain why the current behaviour is a problem and why the expected output is a better solution.]
By changing datatype of a column from int to str, pd.eval throws "TypeError: issubclass() arg 1 must be a class"

Note: We receive a lot of issues on our GitHub tracker, so it is very possible that your issue has been posted before. Please check first before submitting so that we do not have to handle and close duplicates!

Note: Many problems can be resolved by simply upgrading pandas to the latest version. Before submitting, please check if that solution works for you. If possible, you may want to check if master addresses this issue, but that is not necessary.

For documentation-related issues, you can check the latest versions of the docs on master here:

https://pandas-docs.github.io/pandas-docs-travis/

If the issue has not been resolved there, go ahead and file it in the issue tracker.

Expected Output

In Scenario 2

a1 c
10 True
20 False

Output of pd.show_versions()

[paste the output of pd.show_versions() here below this line]
INSTALLED VERSIONS

commit: None
python: 2.7.13.final.0
python-bits: 64
OS: Windows
OS-release: 10
machine: AMD64
processor: Intel64 Family 6 Model 78 Stepping 3, GenuineIntel
byteorder: little
LC_ALL: None
LANG: en
LOCALE: None.None

pandas: 0.21.1
pytest: 3.0.7
pip: 9.0.1
setuptools: 27.2.0
Cython: 0.25.2
numpy: 1.14.2
scipy: 0.19.0
pyarrow: None
xarray: None
IPython: 5.3.0
sphinx: 1.5.6
patsy: 0.4.1
dateutil: 2.6.0
pytz: 2017.2
blosc: None
bottleneck: 1.2.1
tables: 3.2.2
numexpr: 2.6.2
feather: None
matplotlib: 2.0.2
openpyxl: 2.4.7
xlrd: 1.0.0
xlwt: 1.2.0
xlsxwriter: 0.9.6
lxml: 3.7.3
bs4: 4.6.0
html5lib: 0.999
sqlalchemy: 1.1.9
pymysql: None
psycopg2: None
jinja2: 2.9.6
s3fs: None
fastparquet: None
pandas_gbq: None
pandas_datareader: None

@jreback
Copy link
Contributor

jreback commented Jan 23, 2019

this is a duplicate issue

@poeticcode01
Copy link
Author

poeticcode01 commented Jan 24, 2019

can u @jreback provide the solution to handle this?

@mroeschke mroeschke added Bug Numeric Operations Arithmetic, Comparison, and Logical operations labels May 27, 2019
@jbrockmendel jbrockmendel added the expressions pd.eval, query label Oct 22, 2019
@jreback jreback added this to the 1.2 milestone Aug 19, 2020
@simonjayhawkins simonjayhawkins modified the milestones: 1.2, 1.1.2 Aug 27, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug expressions pd.eval, query Numeric Operations Arithmetic, Comparison, and Logical operations
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants