title: Remove outliers (+/- 3 std) and replace with np.nan in Python/pandas
body: <p>I have seen several solutions that come close to solving my problem</p>
  
  <p><a href="https://stackoverflow.com/a/20889300/4638859">link1</a>
  <a href="https://stackoverflow.com/questions/23199796/detect-and-exclude-outliers-in-pandas-dataframe?rq=1">link2</a></p>
  
  <p>but they have not helped me succeed thus far.</p>
  
  <p>I believe that the following solution is what I need, but continue to get an error (and I don't have the reputation points to comment/question on it): <a href="https://stackoverflow.com/questions/27638743/pandas-replace-outliers-with-groupby-mean">link</a> </p>
  
  <p>(I get the following error, but I don't understand where to <code>.copy()</code> or add an "<code>inplace=True</code>" when administering the following command <code>df2=df.groupby('install_site').transform(replace)</code>:</p>
  
  <p>SettingWithCopyWarning: 
  A value is trying to be set on a copy of a slice from a DataFrame.
  Try using <code>.loc[row_indexer,col_indexer] = value</code> instead</p>
  
  <p>See the the caveats in the documentation: <a href="http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy" rel="nofollow noreferrer">link</a></p>
  
  <p>SO, I have attempted to come up with my own version, but I keep getting stuck. Here goes.</p>
  
  <p>I have a data frame indexed by time with columns for site (string values for many different sites) and float values.</p>
  
  <pre><code>time_index            site       val
  </code></pre>
  
  <p>I would like to go through the 'val' column, grouped by site, and replace any outliers (those +/- 3 standard deviations from the mean) with a NaN (for each group).</p>
  
  <p>When I use the following function, I cannot index the data frame with my vector of True/Falses:</p>
  
  <pre><code>def replace_outliers_with_nan(df, stdvs):
      dfnew=pd.DataFrame()
      for i, col in enumerate(df.sites.unique()):
          dftmp = pd.DataFrame(df[df.sites==col])
          idx = [np.abs(dftmp-dftmp.mean())&lt;=(stdvs*dftmp.std())] #boolean vector of T/F's
          dftmp[idx==False]=np.nan  #this is where the problem lies, I believe
          dfnew[col] = dftmp
      return dfnew
  </code></pre>
  
  <p>In addition, I fear the above function will take a very long time on 7 million+ rows, which is why I was hoping to use the groupby function option.</p>