title: Pandas pct_change() using time series and preserving ids
body: <p>I am trying to calculate the perc change occurring between two dates of the values for each id in my df:</p>
  <pre><code>date        id  a  b
  2021-01-01  1   6  4
  2021-01-01  2   10 10
  2021-01-02  1   3  2
  2021-01-02  2   20 20
  </code></pre>
  <p>What I'd like to have as a result is:</p>
  <pre><code>id  ratio_a  ratio_b
  1   -0.5     -0.5
  2   1.0      1.0
  </code></pre>
  <p>I tried playing with <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.pct_change.html" rel="nofollow noreferrer"><code>pct_change()</code></a> but I cannot understand how to use it for this.</p>
  <p>The data for testing can be generated with this code:</p>
  <pre><code>import datetime
  import pandas as pd
  
  dti = pd.to_datetime(
      [
          datetime.datetime(2021, 1, 1),
          datetime.datetime(2021, 1, 1),
          datetime.datetime(2021, 1, 2),
          datetime.datetime(2021, 1, 2)
      ]
  )
  
  d = {
      'id': [1, 2, 1, 2],
      'a': [6, 10, 3, 20],
      'b': [4, 10, 2, 20]
  }
  
  df = pd.DataFrame(data=d, index=dti)
  </code></pre>
  <p>I tried with <code>df.groupby(by=['id']).pct_change()</code> and it gives me:</p>
  <pre><code>
              a   b
  2021-01-01  NaN NaN
  2021-01-01  NaN NaN
  2021-01-02  -0.5    -0.5
  2021-01-02  1.0 1.0
  </code></pre>
  <p>, which is not exactly what I want. I would need a) that dates were sort of aggregated, and b) my ids.</p>