def sample_size(x):
"""
Calculates sample size of a sample x
Args:
x (array_like): sample to calculate sample size
Returns:
int: sample size of the sample excluding nans
"""
# cast into a dummy numpy array to infer the dtype
x_as_array = np.array(x)
if np.issubdtype(x_as_array.dtype, np.number):
_x = np.array(x, dtype=float)
x_nan = np.isnan(_x).sum()
# assuming categorical sample
elif isinstance(x, pd.core.series.Series):
x_nan = x.str.contains('NA').sum()
else:
x_nan = list(x).count('NA')
return len(x) - x_nan
评论列表
文章目录