@@ -591,31 +591,37 @@ As ``group_keys=True`` is the default value of :meth:`DataFrame.groupby` and
591
591
raise a ``FutureWarning ``. This can be silenced and the previous behavior
592
592
retained by specifying ``group_keys=False ``.
593
593
594
- .. _whatsnew_150.notable_bug_fixes .setitem_column_try_inplace :
594
+ .. _whatsnew_150.deprecations .setitem_column_try_inplace :
595
595
_ see also _whatsnew_130.notable_bug_fixes.setitem_column_try_inplace
596
596
597
- Try operating inplace when setting values with ``loc `` and ``iloc ``
598
- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
597
+ Inplace operation when setting values with ``loc `` and ``iloc ``
598
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
599
599
Most of the time setting values with ``frame.iloc `` attempts to set values
600
- in-place, only falling back to inserting a new array if necessary. In the past,
601
- setting entire columns has been an exception to this rule:
600
+ inplace, only falling back to inserting a new array if necessary. There are
601
+ some cases where this rule is not followed, for example when setting an entire
602
+ column from an array with different dtype:
602
603
603
604
.. ipython :: python
604
605
605
- values = np.arange( 4 ).reshape( 2 , 2 )
606
- df = pd.DataFrame(values)
607
- ser = df[ 0 ]
606
+ df = pd.DataFrame({ ' price ' : [ 11.1 , 12.2 ]}, index = [ ' book1 ' , ' book2 ' ] )
607
+ original_prices = df[ ' price ' ]
608
+ new_prices = np.array([ 98 , 99 ])
608
609
609
610
*Old behavior *:
610
611
611
612
.. code-block :: ipython
612
613
613
- In [3]: df.iloc[:, 0] = np.array([10, 11])
614
- In [4]: ser
614
+ In [3]: df.iloc[:, 0] = new_prices
615
+ In [4]: df.iloc[:, 0]
615
616
Out[4]:
616
- 0 0
617
- 1 2
618
- Name: 0, dtype: int64
617
+ book1 98
618
+ book2 99
619
+ Name: price, dtype: int64
620
+ In [5]: original_prices
621
+ Out[5]:
622
+ book1 11.1
623
+ book2 12.2
624
+ Name: price, float: 64
619
625
620
626
This behavior is deprecated. In a future version, setting an entire column with
621
627
iloc will attempt to operate inplace.
@@ -624,39 +630,52 @@ iloc will attempt to operate inplace.
624
630
625
631
.. code-block :: ipython
626
632
627
- In [3]: df.iloc[:, 0] = np.array([10, 11])
628
- In [4]: ser
633
+ In [3]: df.iloc[:, 0] = new_prices
634
+ In [4]: df.iloc[:, 0]
629
635
Out[4]:
630
- 0 10
631
- 1 11
632
- Name: 0, dtype: int64
636
+ book1 98.0
637
+ book2 99.0
638
+ Name: price, dtype: float64
639
+ In [5]: original_prices
640
+ Out[5]:
641
+ book1 98.0
642
+ book2 99.0
643
+ Name: price, dtype: float64
633
644
634
645
To get the old behavior, use :meth: `DataFrame.__setitem__ ` directly:
635
646
636
- *Future behavior *:
637
-
638
647
.. code-block :: ipython
639
648
640
- In [5]: df[0] = np.array([21, 31])
641
- In [4]: ser
642
- Out[4]:
643
- 0 10
644
- 1 11
645
- Name: 0, dtype: int64
646
-
647
- In the case where ``df.columns `` is not unique, use :meth: `DataFrame.isetitem `:
648
-
649
- *Future behavior *:
649
+ In [3]: df[df.columns[0]] = new_prices
650
+ In [4]: df.iloc[:, 0]
651
+ Out[4]
652
+ book1 98
653
+ book2 99
654
+ Name: price, dtype: int64
655
+ In [5]: original_prices
656
+ Out[5]:
657
+ book1 11.1
658
+ book2 12.2
659
+ Name: price, dtype: float64
660
+
661
+ To get the old behaviour when ``df.columns `` is not unique and you want to
662
+ change a single column by index, you can use :meth: `DataFrame.isetitem `, which
663
+ has been added in pandas 1.5:
650
664
651
665
.. code-block :: ipython
652
666
653
- In [5 ]: df.columns = ["A", "A"]
654
- In [5 ]: df .isetitem(0, np.array([21, 31]) )
655
- In [4]: ser
667
+ In [3 ]: df_with_duplicated_cols = pd.concat([df, df], axis='columns')
668
+ In [3 ]: df_with_duplicated_cols .isetitem(0, new_prices )
669
+ In [4]: df_with_duplicated_cols.iloc[:, 0]
656
670
Out[4]:
657
- 0 10
658
- 1 11
659
- Name: 0, dtype: int64
671
+ book1 98
672
+ book2 99
673
+ Name: price, dtype: int64
674
+ In [5]: original_prices
675
+ Out[5]:
676
+ book1 11.1
677
+ book2 12.2
678
+ Name: 0, dtype: float64
660
679
661
680
.. _whatsnew_150.deprecations.numeric_only_default :
662
681
0 commit comments