Skip to content

Commit f0d07ee

Browse files
Fix broadcasting in logical_op function for DataFrame and Series operations
1 parent 7d7c519 commit f0d07ee

File tree

1 file changed

+21
-1
lines changed

1 file changed

+21
-1
lines changed

pandas/core/ops/array_ops.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@
5757
from pandas.core.ops import missing
5858
from pandas.core.ops.dispatch import should_extension_dispatch
5959
from pandas.core.ops.invalid import invalid_comparison
60-
60+
from pandas.core.frame import DataFrame
61+
from pandas.core.series import Series
6162
if TYPE_CHECKING:
6263
from pandas._typing import (
6364
ArrayLike,
@@ -405,6 +406,25 @@ def logical_op(left: ArrayLike, right: Any, op) -> ArrayLike:
405406
-------
406407
ndarray or ExtensionArray
407408
"""
409+
def logical_operator(left: ArrayLike, right: Any, op) -> ArrayLike:
410+
# Check if `right` is a Series and `left` is a DataFrame, and apply broadcasting
411+
if isinstance(left, DataFrame) and isinstance(right, Series):
412+
right = right.values # Convert Series to array for broadcasting
413+
414+
# Convert right to a scalar or valid object if necessary
415+
right = lib.item_from_zerodim(right)
416+
417+
# Check for dtype-less sequences (e.g., list, tuple) and raise error
418+
if is_list_like(right) and not hasattr(right, "dtype"):
419+
# Raise an error if `right` is a list or tuple without a dtype
420+
raise TypeError(
421+
"Logical ops (and, or, xor) between Pandas objects and dtype-less "
422+
"sequences (e.g., list, tuple) are no longer supported. "
423+
"Wrap the object in a Series, Index, or np.array before operating."
424+
)
425+
426+
# Proceed with the logical operation
427+
return logical_operator(op(left, right))
408428

409429
def fill_bool(x, left=None):
410430
# if `left` is specifically not-boolean, we do not cast to bool

0 commit comments

Comments
 (0)