MCPcopy Create free account
hub / github.com/numpy/numpy / _median_nancheck

Function _median_nancheck

numpy/lib/utils.py:1081–1121  ·  view source on GitHub ↗

Utility function to check median result from data for NaN values at the end and return NaN in that case. Input result can also be a MaskedArray. Parameters ---------- data : array Sorted input data to median function result : Array or MaskedArray Result of m

(data, result, axis)

Source from the content-addressed store, hash-verified

1079
1080
1081def _median_nancheck(data, result, axis):
1082 """
1083 Utility function to check median result from data for NaN values at the end
1084 and return NaN in that case. Input result can also be a MaskedArray.
1085
1086 Parameters
1087 ----------
1088 data : array
1089 Sorted input data to median function
1090 result : Array or MaskedArray
1091 Result of median function.
1092 axis : int
1093 Axis along which the median was computed.
1094
1095 Returns
1096 -------
1097 result : scalar or ndarray
1098 Median or NaN in axes which contained NaN in the input. If the input
1099 was an array, NaN will be inserted in-place. If a scalar, either the
1100 input itself or a scalar NaN.
1101 """
1102 if data.size == 0:
1103 return result
1104 potential_nans = data.take(-1, axis=axis)
1105 n = np.isnan(potential_nans)
1106 # masked NaN values are ok, although for masked the copyto may fail for
1107 # unmasked ones (this was always broken) when the result is a scalar.
1108 if np.ma.isMaskedArray(n):
1109 n = n.filled(False)
1110
1111 if not n.any():
1112 return result
1113
1114 # Without given output, it is possible that the current result is a
1115 # numpy scalar, which is not writeable. If so, just return nan.
1116 if isinstance(result, np.generic):
1117 return potential_nans
1118
1119 # Otherwise copy NaNs (if there are any)
1120 np.copyto(result, potential_nans, where=n)
1121 return result
1122
1123def _opt_info():
1124 """

Callers

nothing calls this directly

Calls 3

takeMethod · 0.80
filledMethod · 0.45
anyMethod · 0.45

Tested by

no test coverage detected