Skip to content

BUG: Boolean type cannot be compared with strigs #44903

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
eli-osherovich opened this issue Dec 15, 2021 · 2 comments
Closed

BUG: Boolean type cannot be compared with strigs #44903

eli-osherovich opened this issue Dec 15, 2021 · 2 comments
Labels
Strings String extension data type and string data
Milestone

Comments

@eli-osherovich
Copy link

It seems that Pandas' boolean dtype has some bug -- comparison with strings fails.

Code to reproduce (version 1.3.4)

import pandas as pd
s = pd.Series([True, False, pd.NA], dtype="boolean")
s == "X"

The code above fails with this error:


TypeError                                 Traceback (most recent call last)
<ipython-input-41-f42702e3c612> in <module>
----> 1 a == "X"

~/virtenvs/mkl/lib/python3.9/site-packages/pandas/core/ops/common.py in new_method(self, other)
     67         other = item_from_zerodim(other)
     68 
---> 69         return method(self, other)
     70 
     71     return new_method

~/virtenvs/mkl/lib/python3.9/site-packages/pandas/core/arraylike.py in __eq__(self, other)
     30     @unpack_zerodim_and_defer("__eq__")
     31     def __eq__(self, other):
---> 32         return self._cmp_method(other, operator.eq)
     33 
     34     @unpack_zerodim_and_defer("__ne__")

~/virtenvs/mkl/lib/python3.9/site-packages/pandas/core/series.py in _cmp_method(self, other, op)
   5500 
   5501         with np.errstate(all="ignore"):
-> 5502             res_values = ops.comparison_op(lvalues, rvalues, op)
   5503 
   5504         return self._construct_result(res_values, name=res_name)

~/virtenvs/mkl/lib/python3.9/site-packages/pandas/core/ops/array_ops.py in comparison_op(left, right, op)
    268     ):
    269         # Call the method on lvalues
--> 270         res_values = op(lvalues, rvalues)
    271 
    272     elif is_scalar(rvalues) and isna(rvalues):

~/virtenvs/mkl/lib/python3.9/site-packages/pandas/core/ops/common.py in new_method(self, other)
     67         other = item_from_zerodim(other)
     68 
---> 69         return method(self, other)
     70 
     71     return new_method

~/virtenvs/mkl/lib/python3.9/site-packages/pandas/core/arraylike.py in __eq__(self, other)
     30     @unpack_zerodim_and_defer("__eq__")
     31     def __eq__(self, other):
---> 32         return self._cmp_method(other, operator.eq)
     33 
     34     @unpack_zerodim_and_defer("__ne__")

~/virtenvs/mkl/lib/python3.9/site-packages/pandas/core/arrays/boolean.py in _cmp_method(self, other, op)
    671                 mask = self._mask | mask
    672 
--> 673         return BooleanArray(result, mask, copy=False)
    674 
    675     def _arith_method(self, other, op):

~/virtenvs/mkl/lib/python3.9/site-packages/pandas/core/arrays/boolean.py in __init__(self, values, mask, copy)
    297     def __init__(self, values: np.ndarray, mask: np.ndarray, copy: bool = False):
    298         if not (isinstance(values, np.ndarray) and values.dtype == np.bool_):
--> 299             raise TypeError(
    300                 "values should be boolean numpy array. Use "
    301                 "the 'pd.array' function instead"

TypeError: values should be boolean numpy array. Use the 'pd.array' function instead
@eli-osherovich
Copy link
Author

The same bug exists in 1.3.5

simonjayhawkins added a commit to simonjayhawkins/pandas that referenced this issue Dec 15, 2021
@simonjayhawkins
Copy link
Member

Thanks @eli-osherovich for the report.

The same bug exists in 1.3.5

was fixed in commit: [0f81a63] BUG: BooleanArray raising on comparison to string (#44533)

the fix will be included in the 1.4 release early in the new year.

import pandas as pd

print(pd.__version__)
s = pd.Series([True, False, pd.NA], dtype="boolean")
s == "X"
0    False
1    False
2     <NA>
dtype: boolean

closing as the PR that fixed added tests explicitly for test_eq_mismatched_type and well as replace.

@simonjayhawkins simonjayhawkins added the Strings String extension data type and string data label Dec 15, 2021
@simonjayhawkins simonjayhawkins added this to the 1.4 milestone Dec 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Strings String extension data type and string data
Projects
None yet
Development

No branches or pull requests

2 participants