Skip to content

ERR: better error message on invalid .query input #13139

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
gaow opened this issue May 11, 2016 · 3 comments
Closed

ERR: better error message on invalid .query input #13139

gaow opened this issue May 11, 2016 · 3 comments
Labels
Error Reporting Incorrect or improved errors from pandas
Milestone

Comments

@gaow
Copy link

gaow commented May 11, 2016

Code Sample

import pandas as pd
import numpy as np
df = pd.DataFrame({'A': 'foo bar foo bar foo bar foo foo'.split(),
                   'B': 'one one two three two two one three'.split(),
                   'C': np.arange(8), 'D': np.arange(8) * 2})

Expected output

If I run query with an empty string I should expect no output, however:

>>> df.query('')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/opt/miniconda3/lib/python3.5/site-packages/pandas/core/frame.py", line 2140, in query
    res = self.eval(expr, **kwargs)
  File "/opt/miniconda3/lib/python3.5/site-packages/pandas/core/frame.py", line 2209, in eval
    return _eval(expr, inplace=inplace, **kwargs)
  File "/opt/miniconda3/lib/python3.5/site-packages/pandas/computation/eval.py", line 293, in eval
    return ret
UnboundLocalError: local variable 'ret' referenced before assignment

The problem lies on the last line of https://github.com/pydata/pandas/blob/master/pandas/computation/eval.py

I'm not sure of a proper fix so I'm opening this issue instead of pull request.

output of pd.show_versions()

>>> pd.show_versions()

INSTALLED VERSIONS
------------------
commit: None
python: 3.5.1.final.0
python-bits: 64
OS: Linux
OS-release: 3.16.0-4-amd64
machine: x86_64
processor: 
byteorder: little
LC_ALL: None
LANG: en_US.UTF-8

pandas: 0.18.0
nose: None
pip: 8.1.1
setuptools: 20.3
Cython: None
numpy: 1.11.0
scipy: 0.17.0
statsmodels: None
xarray: None
IPython: 4.2.0
sphinx: None
patsy: None
dateutil: 2.5.2
pytz: 2016.3
blosc: None
bottleneck: None
tables: 3.2.2
numexpr: 2.5.2
matplotlib: 1.5.1
openpyxl: None
xlrd: None
xlwt: None
xlsxwriter: None
lxml: None
bs4: None
html5lib: None
httplib2: None
apiclient: None
sqlalchemy: None
pymysql: None
psycopg2: None
jinja2: 2.8
boto: None
@jreback
Copy link
Contributor

jreback commented May 11, 2016

I supppose this could be a better error message. That is invalid input.

@jreback jreback added Difficulty Novice Error Reporting Incorrect or improved errors from pandas labels May 11, 2016
@jreback jreback added this to the Next Major Release milestone May 11, 2016
@jreback jreback changed the title [BUG] DataFrame.query fails on empty input ERR: better error message on invalid .query input May 11, 2016
@tserafim
Copy link
Contributor

There is already a helper function for that: _check_expression(expr)

Problem is, this function is only executed in the for loop: eval.py#L246-L247

A simple _check_expression(expr) before the for loop can fix it. Or maybe on the beginning of the eval function. Is there a better way, according to pandas code style?

I could send a PR(first OS contribution, ever)

@jreback
Copy link
Contributor

jreback commented Oct 20, 2016

yes u can move it
pls add a test as well

@jreback jreback modified the milestones: 0.19.1, Next Major Release Oct 24, 2016
jorisvandenbossche pushed a commit to jorisvandenbossche/pandas that referenced this issue Nov 2, 2016
… invalid pd.eval and df.query input

closes pandas-dev#13139

Added test case to check for invalid input(empy string) on pd.eval('')
and df.query('').  Used existing helper function(_check_expression)

Author: Thiago Serafim <[email protected]>

Closes pandas-dev#14473 from tserafim/issue#13139 and squashes the following commits:

77483dd [Thiago Serafim] ERR: correctly raise ValueError on empty input to pd.eval() and df.query() (pandas-dev#13139)
9a5c55f [Thiago Serafim] Fix GH13139: better error message on invalid pd.eval and df.query input

(cherry picked from commit 1308884)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Error Reporting Incorrect or improved errors from pandas
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants