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

Function nanmedian

pandas/core/nanops.py:731–835  ·  view source on GitHub ↗

Parameters ---------- values : ndarray axis : int, optional skipna : bool, default True mask : ndarray[bool], optional nan-mask if known Returns ------- result : float | ndarray Unless input is a float array, in which case use the same pr

(
    values: np.ndarray, *, axis: AxisInt | None = None, skipna: bool = True, mask=None
)

Source from the content-addressed store, hash-verified

729
730@bottleneck_switch()
731def nanmedian(
732 values: np.ndarray, *, axis: AxisInt | None = None, skipna: bool = True, mask=None
733) -> float | np.ndarray:
734 """
735 Parameters
736 ----------
737 values : ndarray
738 axis : int, optional
739 skipna : bool, default True
740 mask : ndarray[bool], optional
741 nan-mask if known
742
743 Returns
744 -------
745 result : float | ndarray
746 Unless input is a float array, in which case use the same
747 precision as the input array.
748
749 Examples
750 --------
751 >>> from pandas.core import nanops
752 >>> s = pd.Series([1, np.nan, 2, 2])
753 >>> nanops.nanmedian(s.values)
754 2.0
755
756 >>> s = pd.Series([np.nan, np.nan, np.nan])
757 >>> nanops.nanmedian(s.values)
758 nan
759 """
760 # for floats without mask, the data already uses NaN as missing value
761 # indicator, and `mask` will be calculated from that below -> in those
762 # cases we never need to set NaN to the masked values
763 using_nan_sentinel = values.dtype.kind == "f" and mask is None
764
765 def get_median(x: np.ndarray, _mask=None):
766 if _mask is None:
767 _mask = notna(x)
768 else:
769 _mask = ~_mask
770 if not skipna and not _mask.all():
771 return np.nan
772 with warnings.catch_warnings():
773 # Suppress RuntimeWarning about All-NaN slice
774 warnings.filterwarnings(
775 "ignore", "All-NaN slice encountered", RuntimeWarning
776 )
777 warnings.filterwarnings("ignore", "Mean of empty slice", RuntimeWarning)
778 res = np.nanmedian(x[_mask])
779 return res
780
781 dtype = values.dtype
782 values, mask = _get_values(values, skipna, mask=mask, fill_value=None)
783 if values.dtype.kind != "f":
784 if values.dtype == object:
785 # GH#34671 avoid casting strings to numeric
786 inferred = lib.infer_dtype(values)
787 if inferred in ["string", "mixed"]:
788 raise TypeError(f"Cannot convert {values} to numeric")

Callers

nothing calls this directly

Calls 7

_get_valuesFunction · 0.85
get_medianFunction · 0.85
_wrap_resultsFunction · 0.85
squeezeMethod · 0.80
astypeMethod · 0.45
copyMethod · 0.45

Tested by

no test coverage detected