title: Average of two time series in pandas
body: <h1>Ultimate goal description</h1>
  <p>My goal is to compute the average of two times series (<code>red</code> and <code>green</code>) stored in pandas <code>DataFrame</code>s. However, while both time series have the same columns, they differ in precise time points. What I want to implement is a function <code>average</code> which computes <em>average time series</em> from the two given series such that <em>if a value is missing for particular time point, it should be interpolated.</em> For example:</p>
  <pre><code>import pandas as pd
  green_df = pd.DataFrame({'A': [4, 2, 5], 'B': [1, 2, 3]}, index=[1, 3, 6])
  red_df = pd.DataFrame({'A': [4, 2.5, 8, 2, 4], 'B': [4, 2, 2, 4, 1]}, index=[1, 2, 4, 5, 6])
  
  average_grey_df = pd.DataFrame({'A': [4, 2.7, 3.75, 5.5, 3, 4.5], 'B': [...]}, index= [1, 2, 3, 4, 5, 6])
  
  assert average_grey_df == average(green_df, red_df)
  </code></pre>
  <p>It is obvious when displayed graphically (values shown for column A, but the same should be done with all columns; precise values are just illustrative):</p>
  <p><a href="https://i.stack.imgur.com/0vDtn.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0vDtn.png" alt="enter image description here" /></a></p>
  <h1>Approach</h1>
  <p>So far I was not able to find a completely working solution. I was thinking about dividing it to three steps:</p>
  <p>(1) extend both time series by time points from the other time series such that missing data are <code>nan</code></p>
  <pre><code>                    A  | ...                    A | ...
                  -------                     -------
                  1 | 4 |                     1 | 4 |
                  2 |nan|                     2 |2.5|
      red:        3 | 2 |         green:      3 |nan|
                  4 |nan|                     4 | 8 |
                  5 |nan|                     5 | 2 |
                  6 | 5 |                     6 | 4 |
  </code></pre>
  <p>(2) fill the missing data by interpolating both dataframes (direct usage of <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.interpolate.html" rel="nofollow noreferrer">dataframe interpolate method</a>)
  (3) finally compute average of these two time series as following:</p>
  <pre><code>averages = (green_df.stack() + red_df.stack()) / 2
  average_grey_df = averages.unstack()
  </code></pre>
  <p>Additionally, method <code>dropna</code> can be used to drop created <code>nan</code>s. Moreover, maybe there is a better method I haven't discovered.</p>
  <h1>Question</h1>
  <p>I was not able to figure out how to compute part (1) at all. I checked methods like <code>join</code>, <code>merge</code> and <code>concat</code> with its various examples, but none of them seems to do the job. Any suggestions? I am also open to other approaches.</p>
  <p>Thank you</p>