Skip to content

Bitwise operators are conflated with intersection, union, symmetric difference operators in NB 03.01 #429

Open

Description

@deburg0

In the Section "Index as ordered Set", Bitwise operators are mistakenly equated with Set Operators: intersection, union, symmetric difference.

indA = pd.Index([1, 3, 5, 7, 9])
indB = pd.Index([2, 3, 5, 7, 11])

indA.intersection(indB)
Index([3, 5, 7], dtype='int64')

indA & indB # intersection (actually binary AND)
Index([0, 3, 5, 7, 9], dtype='int64')

Not a big deal, but confusing.

Thank you for your book.

Activity

  1. ollasavana8 commented on Sep 4, 2026

    @ollasavana8

    The example output for indA & indB resulting in Index([0, 3, 5, 7, 9]) confirms that the bitwise AND is operating element-wise on the underlying data rather than treating the index as a set. I can submit a PR to update the text in NB 03.01 to explicitly state that &, |, and ^ perform element-wise arithmetic rather than set operations to prevent this common misconception.

  2. wafir645fcqs49 commented on Sep 16, 2026

    @wafir645fcqs49
  3. christianfrei791 commented on Sep 19, 2026

    @christianfrei791

    This change stems from Pandas 1.0+, which deprecated set operations via &, |, and `

  4. bsoua8745s12 commented on Sep 20, 2026

    @bsoua8745s12
  5. heenll68 commented on Oct 14, 2026

    @heenll68

    The documentation should be updated to note that &, |, and ^ are now strictly bitwise operations in Pandas 1.0+ and explicitly recommend using intersection(), union(), and symmetric_difference() to avoid the misleading results shown in the example.

  6. ShreyasSwasti commented on Oct 19, 2026

    @ShreyasSwasti

    Hi, I added a small note explaining the behavior of the & operator with numeric indexes and why intersection() is useful for finding common elements. I thought this would make the example a little clearer for beginners. Please let me know if any changes are needed. Thanks!

  7. fragrantauto commented on Nov 1, 2026

    @fragrantauto

    The behavior demonstrated in the example confirms that Pandas has shifted away from using bitwise operators for set logic, as noted by @christianfrei791. Since & currently performs a bitwise AND on the underlying integer representations rather than a set intersection, the text should explicitly warn that these operators are now unsafe for set operations in modern Pandas versions. Should we update the notebook to replace these bitwise examples with the explicit intersection, union, and symmetric_difference methods to avoid misleading readers who might be running current environments?

  8. yhdhvyfhv commented on Nov 4, 2026

    @yhdhvyfhv

    The documentation should explicitly note that while & and | perform bitwise operations on pd.Index objects, true set operations now require the intersection and union methods to avoid the unexpected behavior demonstrated in the example.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions