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
519
-
operations (``==``, ``!=``, ...) was inconsistent with the behavior of
520
-
arithmetic operations (``+``, ``-``, ...). The behavior of the comparison
521
-
operations has been changed to match the arithmetic operations in these cases.
522
-
(:issue:`22880`)
523
-
524
-
The affected cases are:
525
-
526
-
- 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`).
527
-
- 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`.
528
-
- 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`).
529
-
530
-
Previous Behavior:
531
-
532
-
.. code-block:: ipython
533
-
534
-
In [3]: arr = np.arange(6).reshape(3, 2)
535
-
In [4]: df = pd.DataFrame(arr)
536
-
537
-
In [5]: df == arr[[0], :]
538
-
...: # comparison previously broadcast where arithmetic would raise
539
-
Out[5]:
540
-
0 1
541
-
0 True True
542
-
1 False False
543
-
2 False False
544
-
In [6]: df + arr[[0], :]
545
-
...
546
-
ValueError: Unable to coerce to DataFrame, shape must be (3, 2): given (1, 2)
547
-
548
-
In [7]: df == (1, 2)
549
-
...: # length matches number of columns;
550
-
...: # comparison previously raised where arithmetic would broadcast
551
-
...
552
-
ValueError: Invalid broadcasting comparison [(1, 2)] with block values
553
-
In [8]: df + (1, 2)
554
-
Out[8]:
555
-
0 1
556
-
0 1 3
557
-
1 3 5
558
-
2 5 7
559
-
560
-
In [9]: df == (1, 2, 3)
561
-
...: # length matches number of rows
562
-
...: # comparison previously broadcast where arithmetic would raise
563
-
Out[9]:
564
-
0 1
565
-
0 False True
566
-
1 True False
567
-
2 False False
568
-
In [10]: df + (1, 2, 3)
569
-
...
570
-
ValueError: Unable to coerce to Series, length must be 2: given 3
571
-
572
-
*Current Behavior*:
573
-
574
-
.. ipython:: python
575
-
:okexcept:
576
-
577
-
arr = np.arange(6).reshape(3, 2)
578
-
df = pd.DataFrame(arr)
579
-
580
-
.. ipython:: python
581
-
# Comparison operations and arithmetic operations both broadcast.
582
-
df == arr[[0], :]
583
-
df + arr[[0], :]
584
-
585
-
.. ipython:: python
586
-
# Comparison operations and arithmetic operations both broadcast.
587
-
df == (1, 2)
588
-
df + (1, 2)
589
-
590
-
.. ipython:: python
591
-
:okexcept:
592
-
# Comparison operations and arithmetic opeartions both raise ValueError.
0 commit comments