Skip to content

SettingWithCopyWarning: possible spurious warning after dropna() call #10288

@jorisvandenbossche

Description

@jorisvandenbossche

I get a SettingWithCopyWarning after the use of dropna, which does not really seem logical:

In [102]: df = pd.DataFrame({'a':[1,2,np.nan], 'b':['A', 'B', 'C']})

In [103]: df
Out[103]:
    a  b
0   1  A
1   2  B
2 NaN  C

In [104]: df = df.dropna(subset=['a'])

In [105]: df
Out[105]:
   a  b
0  1  A
1  2  B

In [106]: df['new_col'] = 2
C:\Anaconda\envs\devel\Scripts\ipython-script.py:1: SettingWithCopyWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/s
table/indexing.html#indexing-view-versus-copy
  if __name__ == '__main__':

In [107]: df.loc[:,'new_col'] = 2
c:\users\vdbosscj\scipy\pandas-joris\pandas\core\indexing.py:404: SettingWithCop
yWarning:
A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_indexer,col_indexer] = value instead

See the the caveats in the documentation: http://pandas.pydata.org/pandas-docs/s
table/indexing.html#indexing-view-versus-copy
  self.obj[item] = s

Further, also the indication were the warning occurred is not correct I think.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions