@@ -591,31 +591,37 @@ As ``group_keys=True`` is the default value of :meth:`DataFrame.groupby` and
591591raise a ``FutureWarning ``. This can be silenced and the previous behavior
592592retained by specifying ``group_keys=False ``.
593593
594- .. _whatsnew_150.notable_bug_fixes .setitem_column_try_inplace :
594+ .. _whatsnew_150.deprecations .setitem_column_try_inplace :
595595 _ see also _whatsnew_130.notable_bug_fixes.setitem_column_try_inplace
596596
597- Try operating inplace when setting values with ``loc `` and ``iloc ``
598- ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
597+ Inplace operation when setting values with ``loc `` and ``iloc ``
598+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
599599Most 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:
602603
603604.. ipython :: python
604605
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 ])
608609
609610 *Old behavior *:
610611
611612.. code-block :: ipython
612613
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]
615616 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
619625
620626 This behavior is deprecated. In a future version, setting an entire column with
621627iloc will attempt to operate inplace.
@@ -624,39 +630,52 @@ iloc will attempt to operate inplace.
624630
625631.. code-block :: ipython
626632
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]
629635 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
633644
634645 To get the old behavior, use :meth: `DataFrame.__setitem__ ` directly:
635646
636- *Future behavior *:
637-
638647.. code-block :: ipython
639648
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:
650664
651665.. code-block :: ipython
652666
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]
656670 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
660679
661680 .. _whatsnew_150.deprecations.numeric_only_default :
662681
0 commit comments