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
667
+
operations (``==``, ``!=``, ...) was inconsistent with the behavior of
668
+
arithmetic operations (``+``, ``-``, ...). The behavior of the comparison
669
+
operations has been changed to match the arithmetic operations in these cases.
670
+
(:issue:`22880`)
671
+
672
+
The affected cases are:
673
+
674
+
- 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`).
675
+
- 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`.
676
+
- 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`).
677
+
678
+
Previous Behavior:
679
+
680
+
.. code-block:: ipython
681
+
682
+
In [3]: arr = np.arange(6).reshape(3, 2)
683
+
In [4]: df = pd.DataFrame(arr)
684
+
685
+
In [5]: df == arr[[0], :]
686
+
...: # comparison previously broadcast where arithmetic would raise
687
+
Out[5]:
688
+
0 1
689
+
0 True True
690
+
1 False False
691
+
2 False False
692
+
In [6]: df + arr[[0], :]
693
+
...
694
+
ValueError: Unable to coerce to DataFrame, shape must be (3, 2): given (1, 2)
695
+
696
+
In [7]: df == (1, 2)
697
+
...: # length matches number of columns;
698
+
...: # comparison previously raised where arithmetic would broadcast
699
+
...
700
+
ValueError: Invalid broadcasting comparison [(1, 2)] with block values
701
+
In [8]: df + (1, 2)
702
+
Out[8]:
703
+
0 1
704
+
0 1 3
705
+
1 3 5
706
+
2 5 7
707
+
708
+
In [9]: df == (1, 2, 3)
709
+
...: # length matches number of rows
710
+
...: # comparison previously broadcast where arithmetic would raise
711
+
Out[9]:
712
+
0 1
713
+
0 False True
714
+
1 True False
715
+
2 False False
716
+
In [10]: df + (1, 2, 3)
717
+
...
718
+
ValueError: Unable to coerce to Series, length must be 2: given 3
719
+
720
+
*Current Behavior*:
721
+
722
+
.. ipython:: python
723
+
:okexcept:
724
+
725
+
arr = np.arange(6).reshape(3, 2)
726
+
df = pd.DataFrame(arr)
727
+
728
+
.. ipython:: python
729
+
# Comparison operations and arithmetic operations both broadcast.
730
+
df == arr[[0], :]
731
+
df + arr[[0], :]
732
+
733
+
.. ipython:: python
734
+
# Comparison operations and arithmetic operations both broadcast.
735
+
df == (1, 2)
736
+
df + (1, 2)
737
+
738
+
.. ipython:: python
739
+
:okexcept:
740
+
# Comparison operations and arithmetic opeartions both raise ValueError.
0 commit comments