Title: BUG: Fix Incorrect Logical Operation Between Pandas DataFrame and Series (#60204) #60278
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR fixes issue #60204, where logical operations (
&
,|
,^
) between aDataFrame
and aSeries
were not correctly broadcasting, leading to unexpected results. The issue was observed when attempting operations like(df >= 10) | (df['A'] >= 10)
, wheredf
is aDataFrame
anddf['A']
is aSeries
.Changes Made
logical_op
function: Added logic to handle cases whereleft
is aDataFrame
andright
is aSeries
. The function now convertsright
to a NumPy array to allow proper broadcasting.TypeError
ifright
is a list-like object (e.g., list, tuple) without a defined dtype, as these are unsupported in logical operations with DataFrames.fill_bool
: Modifiedfill_bool
to ensure proper casting to boolean, accommodating non-boolean types and handling missing values correctly.Code Adjustments
DataFrame
andSeries
types, appliedlib.item_from_zerodim
for handling zero-dimensionalright
objects.right
toright.values
when it's a Series, enabling proper broadcasting across DataFrame columns in logical operations.Testing and Validation
pandas/tests/arithmetic/test_ops.py
to verify:DataFrame
andSeries
to ensure correct broadcasting behavior.TypeError
is raised for unsupported list-like objects without dtype.Additional Notes
Checklist
Related Issue
Closes #60204
Thank you for reviewing this PR! Please let me know if further adjustments are needed.