title: How to calculate the difference of a value for multiple dates?
body: <p>My data set contains multiple values for gas storage. Each of them I want to compare to the value at the exact date one year ago, for multiple years. This is how my data looks like:</p>
  <div class="s-table-container">
  <table class="s-table">
  <thead>
  <tr>
  <th>facility</th>
  <th>gasDayStartedOn</th>
  <th>gasInStorage</th>
  <th>full</th>
  <th>injection</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>UGS Haidach</td>
  <td>2022-01-09</td>
  <td>4.3041</td>
  <td>37</td>
  <td>0.00</td>
  </tr>
  <tr>
  <td>UGS Haidach</td>
  <td>2022-01-08</td>
  <td>4.3263</td>
  <td>38</td>
  <td>0.00</td>
  </tr>
  <tr>
  <td>UGS Haidach</td>
  <td>2021-01-09</td>
  <td>5.5678</td>
  <td>43</td>
  <td>0.00</td>
  </tr>
  </tbody>
  </table>
  </div>
  <p>How can I calculate/compare <code>gasInStorgae</code> for each year <code>gasDayStartedOn</code> for the same facility and store it in a new column within the same <code>DataFrame</code>?</p>
  <p>I have wrote this <code>code</code>:</p>
  <pre><code>def det_dates(df, a_date):
      b_df = df[df.gasDayStartedOn == a_date - pd.Timedelta(days=365)]
      if b_df.shape[0] != 0:
          return b_df.full.values[0]
      return None
  
  def get_dif(df):
      for i, r in df.iterrows():
          a_date = r.gasDayStartedOn
          a_gasInStorage = r.gasInStorage
          b_gasInStorage = det_dates(df, a_date)
          
          if b_gasInStorage:
              dif_gasInStorage = a_gasInStorage - gasInStorage
          else:
              dif_gasInStorage = None
              
          df.loc[i, 'difdif'] = dif_gasInStorage
  
  dfs = []
  
  for com_fac, group in tqdm(data_1.groupby(['company', 'facility'])):
      g = group.copy()
      g.sort_values('gasDayStartedOn', inplace=True, ascending=False)
      get_dif(g)
      dfs.append(g)
  </code></pre>
  <p>But its not working! PLS help! This is the error I get:</p>
  <p><code>from datetime import datetime, timedelta</code></p>