Compute the median of dynamic time series
<p>If I have a pandas series [a1,a2,a3,a4,...] with length = T. Each value corresponds to one day. For each day, I would like to compute the historical median. For example, the first day compute the median of [a1]; the second day compute the median of [a1,a2]; the nth day compute the median of [a1,a2,...,an]. Finally I would like to get a series with length = T as well. Do we have an efficient way to do this in pandas? Thanks!</p>

<p>For a Series, <code>ser</code>:</p>

<pre><code>ser = pd.Series(np.random.randint(0, 100, 10))
</code></pre>

<p>If your pandas version is 0.18.0 or above, use:</p>

<pre><code>ser.expanding().median()
Out: 
0     0.0
1    25.0
2    50.0
3    36.5
4    33.0
5    36.0
6    33.0
7    36.0
8    33.0
9    36.0
dtype: float64
</code></pre>

<p>The following is for earlier versions and deprecated:</p>

<pre><code>pd.expanding_median(ser)
C:\Anaconda3\envs\p3\lib\site-packages\spyder\utils\ipython\start_kernel.py:1: FutureWarning: pd.expanding_median is deprecated for Series and will be removed in a future version, replace with 
        Series.expanding(min_periods=1).median()
  # -*- coding: utf-8 -*-
Out: 
0     0.0
1    25.0
2    50.0
3    36.5
4    33.0
5    36.0
6    33.0
7    36.0
8    33.0
9    36.0
dtype: float64
</code></pre>

python pandas