Mean of empty slice and Degrees of freedom <=0 Mean of empty slice and Degrees of freedom <=0 numpy numpy

Mean of empty slice and Degrees of freedom <=0


RuntimeWarning: Degrees of freedom <= 0 for slice

occurs when you use the wrong shape, e.g.:

import numpy as npx = np.random.random([1000,1])y = np.random.random([1000,1])print(x.shape, y.shape)# (1000, 1) (1000, 1)t = np.cov(x, y) #RuntimeWarningt = np.cov(x.T, y.T) #This works


An edge case is: the array you calculate covariance on only contains one element.

np.cov([0.5])


In addition to the use of wrong shape mentioned above, using np.nanstd across all NaNs array will also invoke the message of "RuntimeWarning: Degrees of freedom <= 0 for slice"; using np.nanmean across all NaNs array will invoke the message of "RuntimeWarning: Mean of empty slice.".