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