title: Logarithmic returns in pandas dataframe groupby ticker
body: <p>I like to calculate the <em>log return</em> based on stock prices (adjclose) for each ticker in a dataframe with several tickers and prices.</p>
  <p>A sample of such a dataframe:</p>
  <pre><code>import pandas as pd
  import numpy as np
  f = {'date': ['2020-11-25', '2020-11-24', '2020-11-23', '2020-11-25', '2020-11-24', '2020-11-23'],
           'ticker': ['AAPL', 'AAPL', 'AAPL', 'AIR', 'AIR', 'AIR'],
           'adjclose': [116.029999, 115.169998, 113.849998, 29.510000, 29.540000, 27.890000]
           }
  
  df = pd.DataFrame(f)
  df.set_index('date', inplace=True)
  </code></pre>
  <p>Dataframe df:</p>
  <pre><code>               ticker    adjclose
  date                             
  2020-11-25       AAPL  116.029999
  2020-11-24       AAPL  115.169998
  2020-11-23       AAPL  113.849998
  2020-11-25        AIR   29.510000
  2020-11-24        AIR   29.540000
  2020-11-23        AIR   27.890000
  </code></pre>
  <p>Following the ideas proposed here <a href="https://stackoverflow.com/questions/31287552/logarithmic-returns-in-pandas-dataframe/48539196">Logarithmic returns in pandas dataframe</a></p>
  <ol>
  <li><p>I would sort the stockprices by ticker and date</p>
  <p><code>df.sort_values(by=['ticker', 'date'], ascending=True, inplace=True)</code></p>
  <p><code>df</code></p>
  </li>
  </ol>
  <blockquote>
  <pre><code>           ticker    adjclose
  date                         
  2020-11-23   AAPL  113.849998
  2020-11-24   AAPL  115.169998
  2020-11-25   AAPL  116.029999
  2020-11-23    AIR   27.890000
  2020-11-24    AIR   29.540000
  2020-11-25    AIR   29.510000
  </code></pre>
  </blockquote>
  <ol start="2">
  <li><p>The normal (non-log) return</p>
  <p><code>df['return'] = df.groupby(['ticker'])['adjclose'].pct_change()</code></p>
  </li>
  </ol>
  <blockquote>
  <pre><code>           ticker    adjclose    return
  date                                   
  2020-11-23   AAPL  113.849998       NaN
  2020-11-24   AAPL  115.169998  0.011594
  2020-11-25   AAPL  116.029999  0.007467
  2020-11-23    AIR   27.890000       NaN
  2020-11-24    AIR   29.540000  0.059161
  2020-11-25    AIR   29.510000 -0.001016
  </code></pre>
  </blockquote>
  <p>-&gt; the results look fine</p>
  <ol start="3">
  <li><p>now log return</p>
  <p>as a quick test:</p>
  <p><code>np.log(df['adjclose']/df['adjclose'].shift(1))</code></p>
  </li>
  </ol>
  <blockquote>
  <pre><code>2020-11-25         NaN
  2020-11-24   -0.007439
  2020-11-23   -0.011528
  2020-11-25   -1.350153
  2020-11-24    0.001016
  2020-11-23   -0.057477
  </code></pre>
  </blockquote>
  <p>seems to work in principle...</p>
  <p>Now let's group it by ticker and store the result in a new column</p>
  <pre><code>`df['log_return'] = np.log(df.groupby(['ticker'])['adjclose'] / df.groupby(['ticker'])['adjclose'].shift(1))`
  </code></pre>
  <p>I get an error:
  <code>ValueError: operands could not be broadcast together with shapes (3,2) (6,)</code></p>
  <p>Can somehow help me on this topic?
  Thank you very much</p>