SettingWithCopyWarning: possible spurious warning after dropna() call
See original GitHub issueI 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:
- Created 8 years ago
- Comments:5 (5 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
.dropna
is just a fancy name for.ix
with an inverse indexere.g.
we could make
dropna
return a copy, though its really just a slicing operation, hence the warning.dropna
hasinplace=False
by default, so this might be a bug. why don’t you rename, reopen (and change the top example)