Getting SettingWithCopyWarning warning even after using .loc in pandas
<pre><code>df_masked.loc[:, col] = df_masked.groupby([df_masked.index.month, df_masked.index.day])[col].\
            transform(lambda y: y.fillna(y.median()))
</code></pre>

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

<pre><code>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
</code></pre>

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

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

<p><a href="http://stackoverflow.com/a/38810015/190597">There are ways</a> to shut off the UserWarning on a per-statement basis. In particular, you could use <code>df_masked.is_copy = False</code>. </p>

<p>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</p>

<pre><code>pd.options.mode.chained_assignment = None
</code></pre>

<p>near the top of your code.</p>

<hr>

<p>Here is a simple example which demonstrate the problem and (a) solution:</p>

<pre><code>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'
</code></pre>

<hr>

<p>The reason why the UserWarning exists is to help alert new users to the fact that
<a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#why-does-assignment-fail-when-using-chained-indexing" rel="nofollow">chained-indexing</a> such as</p>

<pre><code>df.iloc[1:].loc[:, 'swallow'] = 'forest'
</code></pre>

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

python pandas