You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Previously, the broadcasting behavior of :class:`DataFrame` comparison
666
+
operations (``==``, ``!=``, ...) was inconsistent with the behavior of
667
+
arithmetic operations (``+``, ``-``, ...). The behavior of the comparison
668
+
operations has been changed to match the arithmetic operations in these cases.
669
+
(:issue:`22880`)
670
+
671
+
The affected cases are:
672
+
673
+
- operating against a 2-dimensional ``np.ndarray`` with either 1 row or 1 column will now broadcast the same way a ``np.ndarray`` would (:issue:`23000`).
674
+
- a list or tuple with length matching the number of rows in the :class:`DataFrame` will now raise ``ValueError`` instead of operating column-by-column (:issue:`22880`.
675
+
- a list or tuple with length matching the number of columns in the :class:`DataFrame` will now operate row-by-row instead of raising ``ValueError`` (:issue:`22880`).
676
+
677
+
Previous Behavior:
678
+
679
+
.. code-block:: ipython
680
+
681
+
In [3]: arr = np.arange(6).reshape(3, 2)
682
+
In [4]: df = pd.DataFrame(arr)
683
+
684
+
In [5]: df == arr[[0], :]
685
+
...: # comparison previously broadcast where arithmetic would raise
686
+
Out[5]:
687
+
0 1
688
+
0 True True
689
+
1 False False
690
+
2 False False
691
+
In [6]: df + arr[[0], :]
692
+
...
693
+
ValueError: Unable to coerce to DataFrame, shape must be (3, 2): given (1, 2)
694
+
695
+
In [7]: df == (1, 2)
696
+
...: # length matches number of columns;
697
+
...: # comparison previously raised where arithmetic would broadcast
698
+
...
699
+
ValueError: Invalid broadcasting comparison [(1, 2)] with block values
700
+
In [8]: df + (1, 2)
701
+
Out[8]:
702
+
0 1
703
+
0 1 3
704
+
1 3 5
705
+
2 5 7
706
+
707
+
In [9]: df == (1, 2, 3)
708
+
...: # length matches number of rows
709
+
...: # comparison previously broadcast where arithmetic would raise
710
+
Out[9]:
711
+
0 1
712
+
0 False True
713
+
1 True False
714
+
2 False False
715
+
In [10]: df + (1, 2, 3)
716
+
...
717
+
ValueError: Unable to coerce to Series, length must be 2: given 3
718
+
719
+
*Current Behavior*:
720
+
721
+
.. ipython:: python
722
+
:okexcept:
723
+
724
+
arr = np.arange(6).reshape(3, 2)
725
+
df = pd.DataFrame(arr)
726
+
727
+
.. ipython:: python
728
+
# Comparison operations and arithmetic operations both broadcast.
729
+
df == arr[[0], :]
730
+
df + arr[[0], :]
731
+
732
+
.. ipython:: python
733
+
# Comparison operations and arithmetic operations both broadcast.
734
+
df == (1, 2)
735
+
df + (1, 2)
736
+
737
+
.. ipython:: python
738
+
:okexcept:
739
+
# Comparison operations and arithmetic opeartions both raise ValueError.
0 commit comments