You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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')
ifnotisinstance(expr, compat.string_types):
msg="expr must be a string to be evaluated, {0} given"raiseValueError(msg.format(type(expr)))
kwargs['level'] =kwargs.pop('level', 0) +1kwargs['target'] =Noneres=self.eval(expr, **kwargs)
try:
new_data=self.loc[res]
exceptValueError:
# when res is multi-dimensional loc raises, but this is sometimes a# valid querynew_data=self[res]
ifinplace:
self._update_inplace(new_data)
else:
returnnew_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
In current pandas version query@pandas/core/frame.py ( https://github.com/pandas-dev/pandas/commit/20fda2223d5121be3f8204702b5ce1e6037e5b18)
The text was updated successfully, but these errors were encountered:
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.
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.
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
The text was updated successfully, but these errors were encountered: