Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
df = pd.DataFrame([(2, 3), (0, 4), (2, 0), (2, 2)],
...                   columns=['deers', 'goats'])
df.cov()
Out[2]:
deers goats
deers 1.000000 -1.166667
goats -1.166667 2.916667
In [3]:
np.random.seed(42)
df = pd.DataFrame(np.random.randn(800, 4),
                  columns=['p', 'q', 'r', 's'])
df.cov()
Out[3]:
p q r s
p 0.905963 -0.007810 -0.045771 -0.007715
q -0.007810 0.994827 -0.034600 0.039390
r -0.045771 -0.034600 0.992015 0.041050
s -0.007715 0.039390 0.041050 1.019627

Minimum number of periods

In [4]:
np.random.seed(42)
df = pd.DataFrame(np.random.randn(21, 3),
                  columns=['p', 'q', 'r'])
df.loc[df.index[:3], 'p'] = np.nan
df.loc[df.index[3:10], 'q'] = np.nan
df.cov(min_periods=12)
Out[4]:
p q r
p 0.308345 NaN -0.155537
q NaN 1.079345 -0.002485
r -0.155537 -0.002485 0.885058