Skip to content

ENH: Bit operations on dataframes with same shapes #62826

@alexelenter-99

Description

@alexelenter-99

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

When performing bitwise operations &, |, ^, between two DataFrame objects of the same shape, pandas currently aligns by index and column labels rather than performing elementwise operations in a coordinate-to-coordinate manner.

This causes unintuitive behavior when users expect DataFrame operations to behave like Series operations, where & is applied positionally.

>>>  import pandas as pd
>>>  x = pd.DataFrame({
           "a": [True, False, True],
           "b": [10, 20, 30] 
         })
>>> y = pd.DataFrame({
         "c": [False, True, True],
        })
>>> x[["a"]] & y

a | c
-- | --
False | False
False | False
False | False
False | False

Feature Description

Add an option or mechanism to perform coordinate-wise (elementwise) bitwise operations between two DataFrames when their shapes are compatible but labels differ.

Specifically, when performing df1 & df2

Rationale

  • Aligns behavior with user expectations and Series semantics.
  • Reduces surprising “matrix-like” outputs that almost never match intended use. Which may result in a big memory allocation

Alternative Solutions

If both DataFrames are of the same shape, automatically cast them to their underlying .to_numpy() values or equivalent Series-like behavior before applying the operation.

Additional Context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementNeeds TriageIssue that has not been reviewed by a pandas team member

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions