7

I have a Pandas series. I need to get sigma_i, which is the standard deviation of a series up to index i. Is there an existing function which efficiently calculates that?

I noticed that there are the cummax and cummin functions.

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
bbc
  • 1,011
  • 3
  • 13
  • 21

1 Answers1

10

See pandas.expanding_std.

For instance:

import numpy as np
import matplotlib.pyplot as plt
import pandas
%matplotlib inline

data = pandas.Series(np.random.normal(size=37))

full_std = np.std(data)
expand_std = pandas.expanding_std(data, min_periods=1)

fig, ax = plt.subplots()
expand_std.plot(ax=ax, color='k', linewidth=1.5, label='Expanded Std. Dev.')
ax.axhline(y=full_std, color='g', label='Full Value')
ax.legend()

Enter image description here

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
Paul H
  • 59,172
  • 18
  • 144
  • 130
  • Interesting -- thoughts why the last value of `expand_std` isn't exactly equal to `full_std`? Shouldn't those be equivalent? – Vincent Nov 06 '17 at 21:30
  • @Vincent, different default values for the degrees of freedom parameter (`ddof`: for numpy it's 0, for pandas, it's 1) – Paul H Nov 06 '17 at 21:36