Skip to content

query works for string data type columns with missing values but not for boolean indexing #31516

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
tdpetrou opened this issue Jan 31, 2020 · 2 comments · Fixed by #54149
Closed
Assignees
Labels
expressions pd.eval, query ExtensionArray Extending pandas with custom dtypes or arrays. good first issue Needs Tests Unit test(s) needed to prevent regressions Strings String extension data type and string data

Comments

@tdpetrou
Copy link
Contributor

tdpetrou commented Jan 31, 2020

Code Sample, a copy-pastable example if possible

>>> df = pd.DataFrame({'a': ['asdf', None]}, dtype='string')
>>> df.query('a == "asdf"') # works
>>> df[df['a'] == 'asdf'] # fails
>>> df.query('a <= "asdf"') # fails
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-24-1d73f7d1a2fe> in <module>
----> 1 df[df['a'] == 'asdf']

~/Documents/Code Practice/pandas-dev/pandas/pandas/core/frame.py in __getitem__(self, key)
   2784 
   2785         # Do we have a (boolean) 1d indexer?
-> 2786         if com.is_bool_indexer(key):
   2787             return self._getitem_bool_array(key)
   2788 

~/Documents/Code Practice/pandas-dev/pandas/pandas/core/common.py in is_bool_indexer(key)
    132             if is_extension_array_dtype(key.dtype):
    133                 if np.any(key.isna()):
--> 134                     raise ValueError(na_msg)
    135             return True
    136     elif isinstance(key, list):

ValueError: cannot mask with array containing NA / NaN values

Problem description

This is a specific bug found from discussion in #31503. The new string data type should fail for any query call where there are missing values in the evaluated condition. It correctly fails for boolean indexing and for inequality conditions in query

Expected Output

df.query('a == "asdf"') should fail

Output of pd.show_versions()

INSTALLED VERSIONS

commit : None
python : 3.8.1.final.0
python-bits : 64
OS : Darwin
OS-release : 19.2.0
machine : x86_64
processor : i386
byteorder : little
LC_ALL : None
LANG : en_US.UTF-8
LOCALE : en_US.UTF-8

pandas : 0+untagged.1.gce8af21.dirty
numpy : 1.18.1
pytz : 2019.3
dateutil : 2.8.1
pip : 20.0.2
setuptools : 45.1.0.post20200127
Cython : 0.29.14
pytest : None
hypothesis : None
sphinx : None
blosc : None
feather : None
xlsxwriter : None
lxml.etree : 4.5.0
html5lib : None
pymysql : None
psycopg2 : None
jinja2 : 2.10.3
IPython : 7.11.1
pandas_datareader: None
bs4 : None
bottleneck : None
fastparquet : None
gcsfs : None
lxml.etree : 4.5.0
matplotlib : 3.1.1
numexpr : None
odfpy : None
openpyxl : None
pandas_gbq : None
pyarrow : None
pytables : None
pytest : None
pyxlsb : None
s3fs : None
scipy : 1.3.1
sqlalchemy : 1.3.13
tables : None
tabulate : 0.8.3
xarray : None
xlrd : None
xlwt : None
xlsxwriter : None
numba : None

@jbrockmendel jbrockmendel added the expressions pd.eval, query label Feb 25, 2020
@mroeschke mroeschke added Bug ExtensionArray Extending pandas with custom dtypes or arrays. Strings String extension data type and string data labels Apr 26, 2020
@mroeschke
Copy link
Member

Looks to work on main now. Could use a test

@mroeschke mroeschke added good first issue Needs Tests Unit test(s) needed to prevent regressions and removed Bug labels Apr 28, 2023
@juliosanz
Copy link

take

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
expressions pd.eval, query ExtensionArray Extending pandas with custom dtypes or arrays. good first issue Needs Tests Unit test(s) needed to prevent regressions Strings String extension data type and string data
Projects
None yet
4 participants