Skip to content

New behaviour regarding inplace values setting with iloc #47381

Closed
@glemaitre

Description

@glemaitre

In scikit-learn, when testing the pandas nightly build, we got a FutureWarning related to the following deprecation:

https://pandas.pydata.org/docs/dev/whatsnew/v1.5.0.html#try-operating-inplace-when-setting-values-with-loc-and-iloc

We have 2 related questions regarding this deprecation. First, it seems that we cannot reproduce the "Old Behaviour" with the latest available release:

In [1]: import pandas as pd

In [2]: import numpy as np

In [3]: pd.__version__
Out[3]: '1.4.2'

In [4]: values = np.arange(4).reshape(2, 2)
   ...: 
   ...: df = pd.DataFrame(values)
   ...: 
   ...: ser = df[0]

In [5]: df.iloc[:, 0] = np.array([10, 11])

In [6]: ser
Out[6]: 
0    10
1    11
Name: 0, dtype: int64

Is there a reason for not spotting the behaviour shown in the documentation?

The second question (actually it is more a comment to open a discussion) concerns the proposed fix.

It is proposed to use df[df.columns[i]] = newvals instead of the df.iloc[:, i] = newvals.
I personally find this way a bit counterintuitive since the SettingWithCopyWarning proposes to change to df.loc[rows, cols] instead of df[cols][rows] to get the inplace behaviour.

If we consider that both approaches intend for an inplace change, the patterns used for "by position" (i.e. .iloc) or "by label" (i.e. .loc) are really different.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Copy / view semanticsIndexingRelated to indexing on series/frames, not to indexes themselves

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions