title: pandas chained_assignment warning exception handling
body: <p>If you worked in python with pandas you already know the chained_assignment warning when working on slices of dataframes (as e.g. described <a href="https://stackoverflow.com/questions/37841525/correct-way-to-set-value-on-a-slice-in-pandas">here</a>).</p>
  
  <p>I found the option <code>pandas.options.mode.chained_assignment</code> which can be set to </p>
  
  <ul>
  <li><code>None</code>, ignoring the warning</li>
  <li><code>"warn"</code>, printing a warning message</li>
  <li><code>"raise"</code>, raising an exception</li>
  </ul>
  
  <p>compare with <a href="https://pandas.pydata.org/pandas-docs/stable/generated/pandas.set_option.html" rel="noreferrer">documentation</a></p>
  
  <p>I included a minimal example, triggering this warning within a <code>try..except..else</code> block for exception handling. I expect an exception to be triggered only with the setting <code>pandas.options.mode.chained_assignment = "raise"</code>, as shown in Example 3 below.</p>
  
  <p>In this minimal example, the behaviour is as expected, so that Example 2, with 
  <code>pandas.options.mode.chained_assignment = "warn"</code> only causes a warning message to be printed, but no exception is raised.</p>
  
  <p>However in a larger framework, I see an exception being raised even though the parameter is set to <code>pandas.options.mode.chained_assignment = "warn"</code>, checked with a print before like in the minimal example (see Example 4)</p>
  
  <p><strong>Is there any other pandas parameter influencing the behavior of this warning message concerning exception raising?</strong></p>
  
  <hr>
  
  <p>Here is a minimal example of code, setting/printing the <code>pd.options.mode.chained_assignment</code> parameter and showing the behaviour in a <code>try..catch..except</code> block.</p>
  
  <pre><code>import pandas as pd
  
  # set the chained_assignment option
  pd.options.mode.chained_assignment = "raise" # raises exception in case of warning
  pd.options.mode.chained_assignment = "warn"  # prints warning in case of warning, no exception is raised
  pd.options.mode.chained_assignment = None    # no warning message and no exception is raised
  
  print "pd.options.mode.chained_assignment :", pd.options.mode.chained_assignment
  
  # create a default pandas dataframe with two columns A,B
  df = pd.DataFrame({"A" : [0, 1, 2], "B" : [3, 4, 5]})
  
  print df
  
  # exctract a slice of the given pandas dataframe
  df2 = df[df["A"] &gt; 0]
  
  # exception handling
  try :
      # try to modify the slice, triggering the pandas warning
      df2["C"] = 2
  except :
      print "EXCEPTION RAISED"
  else :
      print "NO EXCEPTION"
  
  print df2
  </code></pre>
  
  <hr>
  
  <p>Example 1
  Setting <code>pd.options.mode.chained_assignment = None</code> results in the following output (no warning, no exception)</p>
  
  <pre><code>pd.options.mode.chained_assignment : None
     A  B
  0  0  3
  1  1  4
  2  2  5
  NO EXCEPTION
     A  B  C
  1  1  4  2
  2  2  5  2
  </code></pre>
  
  <hr>
  
  <p>Example 2
  Setting <code>pd.options.mode.chained_assignment = "warn"</code> results in the following output (a warning is printed, but no exception)</p>
  
  <pre><code>pd.options.mode.chained_assignment : warn
     A  B
  0  0  3
  1  1  4
  2  2  5
  NO EXCEPTION
  C:\Users\my.name\my\directory\test.py:14: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
    df2["C"] = 2
     A  B  C
  1  1  4  2
  2  2  5  2
  </code></pre>
  
  <hr>
  
  <p>Example 3
  Setting <code>pd.options.mode.chained_assignment = "raise"</code> results in the following output (an exception is raised)</p>
  
  <pre><code>pd.options.mode.chained_assignment : raise
  A  B
  0  0  3
  1  1  4
  2  2  5
  EXCEPTION RAISED
     A  B  C
  1  1  4  2
  2  2  5  2
  </code></pre>
  
  <hr>
  
  <p>Example 4
  This is what I see in a larger framework with exactly the same test code. I do not set the chained <code>pd.options.mode.chained_assignment</code> parameter explictly, but I see it's set to <code>"warn"</code>, even though an exception is raised</p>
  
  <pre><code>pd.options.mode.chained_assignment warn
     A  B
  0  0  3
  1  1  4
  2  2  5
  EXCEPTION RAISED
     A  B  C
  1  1  4  2
  2  2  5  2
  </code></pre>