Skip to content

Commit 7188634

Browse files
committed
Fixed logical op bug for numpy string arrays
1 parent bb906bb commit 7188634

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

pandas/core/ops/array_ops.py

+7
Original file line numberDiff line numberDiff line change
@@ -435,6 +435,13 @@ def fill_bool(x, left=None):
435435
rvalues = right
436436

437437
if should_extension_dispatch(lvalues, rvalues):
438+
# Must cast if logical op between a boolean array and numpy-backed string array
439+
if ((lvalues.dtype == np.bool_ and rvalues.dtype == "string[python]")
440+
or (lvalues.dtype == "string[python]" and rvalues.dtype == np.bool_)
441+
):
442+
lvalues = lvalues.astype(bool)
443+
rvalues = rvalues.astype(bool)
444+
438445
# Call the method on lvalues
439446
res_values = op(lvalues, rvalues)
440447

pandas/tests/arrays/string_/test_string.py

+25-1
Original file line numberDiff line numberDiff line change
@@ -766,4 +766,28 @@ def test_xor_pyarrow_string(dtype):
766766
ser2 = pd.Series(["", "b"], dtype=dtype)
767767
result = ser1 ^ ser2
768768
expected = pd.Series([False, True], dtype=bool)
769-
tm.assert_series_equal(result, expected)
769+
tm.assert_series_equal(result, expected)
770+
771+
@pytest.mark.parametrize("dtype", ["string[python]"])
772+
def test_or_numpy_string(dtype):
773+
ser1 = pd.Series([False, False])
774+
ser2 = pd.Series(["", "b"], dtype=dtype)
775+
result = ser1 | ser2
776+
expected = pd.Series([False, True], dtype=bool)
777+
tm.assert_series_equal(result, expected)
778+
779+
@pytest.mark.parametrize("dtype", ["string[python]"])
780+
def test_and_numpy_string(dtype):
781+
ser1 = pd.Series([False, False])
782+
ser2 = pd.Series(["", "b"], dtype=dtype)
783+
result = ser1 & ser2
784+
expected = pd.Series([False, False], dtype=bool)
785+
tm.assert_series_equal(result, expected)
786+
787+
@pytest.mark.parametrize("dtype", ["string[python]"])
788+
def test_xor_numpy_string(dtype):
789+
ser1 = pd.Series([False, False])
790+
ser2 = pd.Series(["", "b"], dtype=dtype)
791+
result = ser1 ^ ser2
792+
expected = pd.Series([False, True], dtype=bool)
793+
tm.assert_series_equal(result, expected)

0 commit comments

Comments
 (0)