Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting SettingWithCopyWarning warning even after using .loc in pandas [duplicate]

Tags:

python

pandas

df_masked.loc[:, col] = df_masked.groupby([df_masked.index.month, df_masked.index.day])[col].\
            transform(lambda y: y.fillna(y.median()))

Even after using a .loc, I get the foll. error, how do I fix it?

Anaconda\lib\site-packages\pandas\core\indexing.py:476: 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 caveats in the documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy
  self.obj[item] = s
like image 596
user308827 Avatar asked Oct 14 '16 01:10

user308827


People also ask

How to avoid the pandas settingwithcopy warning?

Most of the scenarios of the SettingWithCopy warning can be avoided by communicating to Pandas clearly. When you want to modify the original dataframe, use .loc or when you want a copy, specify it directly. This will not only prevent future warnings and errors, it will also make your codebase more robust for maintenance.

How do I suppress settingwithcopywarning?

A value is trying to be set on a copy of a slice from a DataFrame. One approach that can be used to suppress SettingWithCopyWarning is to perform the chained operations into just a single loc operation. This will ensure that the assignment happens on the original DataFrame instead of a copy.

How to avoid settingwithcopywarning when creating a new column?

You could either complete = train.dropna ().reset_index () or Pandas .assign () will avoid SettingWithCopyWarning and is the recommended way of creating new columns, returning a new data frame object. Your example: Thanks for contributing an answer to Stack Overflow!

How do I suppress this warning in a Dataframe?

The easiest way to suppress this warning is to use the following bit of code: The following example shows how to address this warning in practice. Now suppose we create a new DataFrame that only contains column ‘A’ from the original DataFrame and we divide each value in column ‘A’ by 2:


1 Answers

You could get this UserWarning if df_masked is a sub-DataFrame of some other DataFrame. In particular, if data had been copied from the original DataFrame to df_masked then, Pandas emits the UserWarning to alert you that modifying df_masked will not affect the original DataFrame.

If you do not intend to modify the original DataFrame, then you are free to ignore the UserWarning.

There are ways to shut off the UserWarning on a per-statement basis. In particular, you could use df_masked.is_copy = False.

If you run into this UserWarning a lot, then instead of silencing the UserWarnings one-by-one, I think it is better to leave them be as you are developing your code. Be aware of what the UserWarning means, and if the modifying-the-child-does-not-affect-the-parent issue does not affect you, then ignore it. When your code is ready for production, or if you are experienced enough to not need the warnings, shut them off entirely with

pd.options.mode.chained_assignment = None

near the top of your code.


Here is a simple example which demonstrate the problem and (a) solution:

import pandas as pd

df = pd.DataFrame({'swallow':['African','European'], 'cheese':['gouda', 'cheddar']})
df_masked = df.iloc[1:]
df_masked.is_copy = False   # comment-out this line to see the UserWarning
df_masked.loc[:, 'swallow'] = 'forest'

The reason why the UserWarning exists is to help alert new users to the fact that chained-indexing such as

df.iloc[1:].loc[:, 'swallow'] = 'forest'

will not affect df when the result of the first indexer (e.g. df.iloc[1:]) returns a copy.

like image 73
unutbu Avatar answered Oct 10 '22 03:10

unutbu