MCPcopy
hub / github.com/pandas-dev/pandas / nanmean

Function nanmean

pandas/core/nanops.py:663–727  ·  view source on GitHub ↗

Compute the mean of the element along an axis ignoring NaNs Parameters ---------- values : ndarray axis : int, optional skipna : bool, default True mask : ndarray[bool], optional nan-mask if known Returns ------- float Unless input is a floa

(
    values: np.ndarray,
    *,
    axis: AxisInt | None = None,
    skipna: bool = True,
    mask: npt.NDArray[np.bool_] | None = None,
)

Source from the content-addressed store, hash-verified

661@bottleneck_switch()
662@_datetimelike_compat
663def nanmean(
664 values: np.ndarray,
665 *,
666 axis: AxisInt | None = None,
667 skipna: bool = True,
668 mask: npt.NDArray[np.bool_] | None = None,
669) -> float:
670 """
671 Compute the mean of the element along an axis ignoring NaNs
672
673 Parameters
674 ----------
675 values : ndarray
676 axis : int, optional
677 skipna : bool, default True
678 mask : ndarray[bool], optional
679 nan-mask if known
680
681 Returns
682 -------
683 float
684 Unless input is a float array, in which case use the same
685 precision as the input array.
686
687 Examples
688 --------
689 >>> from pandas.core import nanops
690 >>> s = pd.Series([1, 2, np.nan])
691 >>> nanops.nanmean(s.values)
692 np.float64(1.5)
693 """
694 if values.dtype == object and len(values) > 1_000 and mask is None:
695 # GH#54754 if we are going to fail, try to fail-fast
696 nanmean(values[:1000], axis=axis, skipna=skipna)
697
698 dtype = values.dtype
699 values, mask = _get_values(values, skipna, fill_value=0, mask=mask)
700 dtype_sum = _get_dtype_max(dtype)
701 dtype_count = np.dtype(np.float64)
702
703 # not using needs_i8_conversion because that includes period
704 if dtype.kind in "mM":
705 dtype_sum = np.dtype(np.float64)
706 elif dtype.kind in "iu":
707 dtype_sum = np.dtype(np.float64)
708 elif dtype.kind == "f":
709 dtype_sum = dtype
710 dtype_count = dtype
711
712 count = _get_counts(values.shape, mask, axis, dtype=dtype_count)
713 the_sum = values.sum(axis, dtype=dtype_sum)
714 the_sum = _ensure_numeric(the_sum)
715
716 if axis is not None and getattr(the_sum, "ndim", False):
717 count = cast(np.ndarray, count)
718 with np.errstate(all="ignore"):
719 # suppress division by zero warnings
720 the_mean = the_sum / count

Callers

nothing calls this directly

Calls 7

_get_valuesFunction · 0.85
_get_dtype_maxFunction · 0.85
_get_countsFunction · 0.85
_ensure_numericFunction · 0.85
dtypeMethod · 0.45
sumMethod · 0.45
anyMethod · 0.45

Tested by

no test coverage detected