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

Closed
gaow opened this Issue May 11, 2016 · 3 comments

Comments

Projects
None yet
3 participants

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
Contributor

jreback commented May 11, 2016 edited

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

jreback added this to the Next Major Release milestone May 11, 2016

jreback changed the title from [BUG] DataFrame.query fails on empty input to ERR: better error message on invalid .query input May 11, 2016

Contributor

tserafim commented Oct 20, 2016

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)

Contributor

jreback commented Oct 20, 2016

yes u can move it
pls add a test as well

@jreback jreback modified the milestone: 0.19.1, Next Major Release Oct 24, 2016

@tserafim tserafim added a commit to tserafim/pandas that referenced this issue Oct 24, 2016

@tserafim tserafim ERR: correctly raise ValueError on empty input to pd.eval() and df.qu…
…ery() (#13139)
77483dd

jreback closed this in 1308884 Oct 24, 2016

@jorisvandenbossche jorisvandenbossche added a commit to jorisvandenbossche/pandas that referenced this issue Nov 2, 2016

@tserafim @jorisvandenbossche tserafim + jorisvandenbossche [Backport #14473] ERR: Fix GH13139: better error message on invalid p…
…d.eval and df.query input


closes #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 <thiago.serafim@gmail.com>

Closes #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() (#13139)
9a5c55f [Thiago Serafim] Fix GH13139: better error message on invalid pd.eval and df.query input

(cherry picked from commit 1308884)
6ff4108

@amolkahat amolkahat added a commit to amolkahat/pandas that referenced this issue Nov 26, 2016

@tserafim @amolkahat tserafim + amolkahat ERR: Fix GH13139: better error message on invalid pd.eval and df.quer…
…y input


closes #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 <thiago.serafim@gmail.com>

Closes #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() (#13139)
9a5c55f [Thiago Serafim] Fix GH13139: better error message on invalid pd.eval and df.query input
aad7ce3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment