Skip to content

Commit 2e9d547

Browse files
committed
updates
1 parent c56894e commit 2e9d547

File tree

2 files changed

+13
-59
lines changed

2 files changed

+13
-59
lines changed

pandas/core/arrays/boolean.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -575,10 +575,11 @@ def logical_method(self, other):
575575
raise NotImplementedError(
576576
"can only perform ops with 1-d structures"
577577
)
578-
if len(self) != len(other):
579-
raise ValueError("Lengths must match to compare")
580578
other, mask = coerce_to_array(other, copy=False)
581579

580+
if not lib.is_scalar(other) and len(self) != len(other):
581+
raise ValueError("Lengths must match to compare")
582+
582583
if op.__name__ in {"or_", "ror_"}:
583584
result, mask = kleene_or(self._data, other, self._mask, mask)
584585
return BooleanArray(result, mask)

pandas/tests/arrays/test_boolean.py

Lines changed: 10 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -360,45 +360,19 @@ def get_op_from_name(self, op_name):
360360

361361
return op
362362

363-
def _compare_other(self, data, op_name, other):
364-
op = self.get_op_from_name(op_name)
365-
366-
# array
367-
result = pd.Series(op(data, other))
368-
expected = pd.Series(op(data._data, other), dtype="boolean")
369-
370-
# fill the nan locations
371-
expected[data._mask] = np.nan
372-
373-
tm.assert_series_equal(result, expected)
374-
375-
# series
376-
s = pd.Series(data)
377-
result = op(s, other)
378-
379-
expected = pd.Series(data._data)
380-
expected = op(expected, other)
381-
expected = pd.Series(expected, dtype="boolean")
382-
383-
# fill the nan locations
384-
expected[data._mask] = np.nan
363+
def test_logical_length_mismatch_raises(self, all_logical_operators):
364+
op_name = all_logical_operators
365+
a = pd.array([True, False, None], dtype="boolean")
366+
msg = "Lengths must match to compare"
385367

386-
tm.assert_series_equal(result, expected)
368+
with pytest.raises(ValueError, match=msg):
369+
getattr(a, op_name)([True, False])
387370

388-
def test_scalar(self, data, all_logical_operators):
389-
op_name = all_logical_operators
390-
self._compare_other(data, op_name, True)
371+
with pytest.raises(ValueError, match=msg):
372+
getattr(a, op_name)(np.array([True, False]))
391373

392-
def test_array(self, data, all_logical_operators):
393-
op_name = all_logical_operators
394-
if "or" in op_name:
395-
pytest.skip("confusing")
396-
other = pd.array([True] * len(data), dtype="boolean")
397-
self._compare_other(data, op_name, other)
398-
other = np.array([True] * len(data))
399-
self._compare_other(data, op_name, other)
400-
other = pd.Series([True] * len(data), dtype="boolean")
401-
self._compare_other(data, op_name, other)
374+
with pytest.raises(ValueError, match=msg):
375+
getattr(a, op_name)(pd.array([True, False], dtype="boolean"))
402376

403377
def test_kleene_or(self):
404378
# A clear test of behavior.
@@ -431,27 +405,6 @@ def test_kleene_or_scalar(self, other, expected):
431405
result = other | a
432406
tm.assert_extension_array_equal(result, expected)
433407

434-
@pytest.mark.parametrize(
435-
"left,right,expected",
436-
[
437-
([True, False, None], True, [True, True, True]),
438-
([True, False, None], False, [True, False, None]),
439-
([True, False, None], np.nan, [True, None, None]),
440-
# TODO: pd.NA
441-
],
442-
)
443-
def test_kleene_or_cases(self, left, right, expected):
444-
if isinstance(left, list):
445-
left = pd.array(left, dtype="boolean")
446-
if isinstance(right, list):
447-
right = pd.array(right, dtype="boolean")
448-
expected = pd.array(expected, dtype="boolean")
449-
result = left | right
450-
tm.assert_extension_array_equal(result, expected)
451-
452-
result = right | left
453-
tm.assert_extension_array_equal(result, expected)
454-
455408
def test_kleene_and(self):
456409
# A clear test of behavior.
457410
a = pd.array([True] * 3 + [False] * 3 + [None] * 3, dtype="boolean")

0 commit comments

Comments
 (0)