question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SettingWithCopyWarning: possible spurious warning after dropna() call

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

3reactions
jrebackcommented, Jun 5, 2015

.dropna is just a fancy name for .ix with an inverse indexer

e.g.

In [30]: df.ix[pd.notnull(df.a)]
Out[30]: 
   a  b
0  1  A
1  2  B

In [31]: df.ix[pd.notnull(df.a)].is_copy
Out[31]: <weakref at 0x121aa7f70; to 'DataFrame' at 0x121a75c90>

we could make dropna return a copy, though its really just a slicing operation, hence the warning

0reactions
jrebackcommented, Jun 5, 2015

.dropna has inplace=False by default, so this might be a bug. why don’t you rename, reopen (and change the top example)

Read more comments on GitHub >

github_iconTop Results From Across the Web

SettingwithCopyWarning: How to Fix This Warning in Pandas
Pandas generates the warning when it detects something called chained assignment. Let's define a few terms we'll be using to explain things:.
Read more >
Misleading SettingWithCopyWarning when assigning on ...
The SettingWithCopyWarning is raised because there is potential ambiguity in value assignment. When you assigned values to df2["A"] ...
Read more >
SettingWithCopyWarning in Pandas: Views vs Copies
Pandas sometimes issues a SettingWithCopyWarning to warn the user of a potentially inappropriate use of views and copies. In this article, you'll learn:....
Read more >
3 ways to deal with SettingWithCopyWarning in Pandas
One of the most common reasons Pandas generates this warning is when it detects chained assignment or chained indexing. There are two things...
Read more >
What's new in 1.4.0 (January 22, 2022) - Pandas
Previously, warning messages may have pointed to lines within the pandas library. ... DataFrame.dropna() now accepts a single label as subset along with ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found