Confusion re: pandas copy of slice of dataframe warning
<p>I've looked through a bunch of questions and answers related to this issue, but I'm still finding that I'm getting this copy of slice warning in places where I don't expect it. Also, it's cropping up in code that was running fine for me previously, leading me to wonder if some sort of update may be the culprit.  </p>

<p>For example, this is a set of code where all I'm doing is reading in an Excel file into a pandas <code>DataFrame</code>, and cutting down the set of columns included with the <code>df[[]]</code> syntax.  </p>

<pre><code> izmir = pd.read_excel(filepath)
 izmir_lim = izmir[['Gender','Age','MC_OLD_M&gt;=60','MC_OLD_F&gt;=60','MC_OLD_M&gt;18','MC_OLD_F&gt;18','MC_OLD_18&gt;M&gt;5','MC_OLD_18&gt;F&gt;5',
               'MC_OLD_M_Child&lt;5','MC_OLD_F_Child&lt;5','MC_OLD_M&gt;0&lt;=1','MC_OLD_F&gt;0&lt;=1','Date to Delivery','Date to insert','Date of Entery']]
</code></pre>

<p>Now, any further changes I make to this <code>izmir_lim</code> file raise the copy of slice warning.  </p>

<pre><code>izmir_lim['Age'] = izmir_lim.Age.fillna(0)
izmir_lim['Age'] = izmir_lim.Age.astype(int)
</code></pre>

<blockquote>
  <p>/Users/samlilienfeld/anaconda/lib/python3.5/site-packages/ipykernel/<strong>main</strong>.py:2:
  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</p>
</blockquote>

<p>I'm confused because I thought the <code>df[[]]</code> column subsetting returned a copy by default.  The only way I've found to suppress the errors is by explicitly adding <code>df[[]].copy()</code>. I could have sworn that in the past I did not have to do that and did not raise the copy of slice error.</p>

<p>Similarly, I have some other code that runs a function on a dataframe to filter it in certain ways:</p>

<pre><code>def lim(df):
if (geography == "All"):
    df_geo = df
else:
    df_geo = df[df.center_JO == geography]

df_date = df_geo[(df_geo.date_survey &gt;= start_date) &amp; (df_geo.date_survey &lt;= end_date)]

return df_date

df_lim = lim(df)
</code></pre>

<p>From this point forward, any changes I make to any of the values of <code>df_lim</code> raise the copy of slice error. The only way around it that i've found is to change the function call to:</p>

<pre><code>df_lim = lim(df).copy()
</code></pre>

<p>This just seems wrong to me. What am I missing? It seems like these use cases should return copies by default, and I could have sworn that the last time I ran these scripts I was not running in to these errors.<br>
Do I just need to start adding <code>.copy()</code> all over the place? Seems like there should be a cleaner way to do this. Any insight or help is much appreciated.</p>