title: Subtracting vector from vector and np.roll the result in Python
body: <p>I have a vector of susceptible areas and a vector of newly infected areas for every year in my model. The rows indicate different population ages and the columns represent different years. I am trying to "age" the susceptible vectors by using np.roll. In the case of an epidemic, I need to subtract the newly-infected-area-vector and "age" the remainder. In earlier drafts, this was not a problem, however, for some reason it does not work anymore and results in </p>
  
  <pre><code>__main__:172: SettingWithCopyWarning:  A value is trying to be set on a copy of a slice from a DataFrame
  </code></pre>
  
  <p>A small illustration of what I am trying to do is below</p>
  
  <pre><code>import numpy as np
  import pandas as pd
  
  years = range(10) 
  # Vector1 illustrates the susceptible individuals (n) indicates the different ages
  vector1 = [100 for n in range(100)]  
  # Vector2 illustrates the newly infected individuals across ages. This constant should be subtracted for every year from the remainder at t-1.
  vector2 = [10 for n in range(100)] 
  # In this df, I would like to store the remainder of susceptible individuals. Rows indicate the possible ages (n), columns the point in time (t).
  df = pd.DataFrame([[0 for t in years] for n in range(100)])
  
  for n in range(100):
      for t in years:
          # In the first year, no infection occurs and the column is just the vector of susceptible individuals
          if t == 0:
              df[n] = vector1 
          # In t+1 the epidemic starts and for every year I want to subtract the newly infected individuals (vector2) from the vector of susceptible (and not yet infected) individuals which I have calculated at t-1
          elif t-1 &gt;= 0:
              df[t][n] = np.roll(np.subtract(df[t-1][n], vector2[n]), shift=1)
  </code></pre>