In function query@pandas/core/frame.py
I found dataframe return eval result, and use self.loc to return new dataframe, and I curious about in which situation dataframe.loc will raise ValueError.
inplace = validate_bool_kwarg(inplace, 'inplace')
if not isinstance(expr, compat.string_types):
msg = "expr must be a string to be evaluated, {0} given"
raise ValueError(msg.format(type(expr)))
kwargs['level'] = kwargs.pop('level', 0) + 1
kwargs['target'] = None
res = self.eval(expr, **kwargs)
try:
new_data = self.loc[res]
except ValueError:
# when res is multi-dimensional loc raises, but this is sometimes a
# valid query
new_data = self[res]
if inplace:
self._update_inplace(new_data)
else:
return new_data
Problem description
Because I use Dataframe.query() as data_model api, and I wanna select columns from this api, so I do some googling, found nothing. After read code of query, I found interesting fragment in function query. If loc raise Exception, select column instead. BUT, it catch ValueError, not KeyError.
Version
Comment From: jorisvandenbossche
@dingo9 Please provide a minimal, reproducible example (runnable code snippet) illustrating the problem you have.
Comment From: dingo9
import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(6,4),columns=list('ABCD'))
df.query("['A']")
I expect it return columns 'A' in dataframe, but it raise KeyError at
try:
new_data = self.loc[res]
except ValueError:
# when res is multi-dimensional loc raises, but this is sometimes a
# valid query
new_data = self[res]
In such fragment, if it raise ValueError, it will return columns 'A' as I expect.
Comment From: jreback
pls read the docs: http://pandas.pydata.org/pandas-docs/stable/indexing.html#the-query-method-experimental
In [7]: df = pd.DataFrame(np.random.randn(6,4),columns=list('ABCD'))
...: df.query("A>0")
Out[7]:
A B C D
5 1.105172 1.219918 -1.555644 0.937555
In [8]: df.eval('A')
Out[8]:
0 -1.534979
1 -0.328682
2 -0.181169
3 -1.026832
4 -0.984711
5 1.105172
dtype: float64