Skip to content

Pandas vs Numpy broadcasting discrepancy #22686

Closed
@timlod

Description

@timlod

Code Sample, a copy-pastable example if possible

# Not working in full pandas
pd.DataFrame(np.full((5,5), True)) & np.full((1,5), False)
#ValueError: Unable to coerce to DataFrame, shape must be (5, 5): given (1, 5)

# After extracting the numpy array it works as expected:
pd.DataFrame(np.full((5,5), True)).values & np.full((1,5), False)

Problem description

I have a dataframe with shape (m,n), each column filled with bools.
Now I have a numpy (column) vector of length n filled with bools and I would like to apply the logical AND (&) such that the operation is broadcast from the vector onto each column.
This works only when using df.values, not when applying & on the dataframe.

The same holds for other operations such as *, /
I am not sure whether this is intended behaviour - I could not find the matching documentation.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Numeric OperationsArithmetic, Comparison, and Logical operations

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions