Enforcing compatibility between numpy 1.8 and 1.9 nansum? -
i have code needs behave identically independent of numpy version, underlying np.nansum
function has changed behavior such np.nansum([np.nan,np.nan])
0.0
in 1.9 , nan
in 1.8. <=1.8 behavior 1 prefer, more of import thing code robust against numpy version.
the tricky thing is, code applies arbitrary numpy function (generally, np.nan[something]
function) ndarray. there way forcefulness new or old numpy nan[something]
functions conform old or new behavior shy of monkeypatching them?
a possible solution can think of outarr[np.allnan(inarr, axis=axis)] = np.nan
, there no np.allnan
function - if best solution, best implementation np.all(np.isnan(arr), axis=axis)
(which require supporting np>=1.7, that's ok)?
in numpy 1.8, nansum
defined as:
a, mask = _replace_nan(a, 0) if mask none: homecoming np.sum(a, axis=axis, dtype=dtype, out=out, keepdims=keepdims) mask = np.all(mask, axis=axis, keepdims=keepdims) tot = np.sum(a, axis=axis, dtype=dtype, out=out, keepdims=keepdims) if np.any(mask): tot = _copyto(tot, np.nan, mask) warnings.warn("in numpy 1.9 sum along empty slices zero.", futurewarning) homecoming tot
in numpy 1.9, is:
class="lang-py prettyprint-override">a, mask = _replace_nan(a, 0) homecoming np.sum(a, axis=axis, dtype=dtype, out=out, keepdims=keepdims)
i don't think there way create new nansum
behave old way, given original nansum
code isn't long, can include re-create of code (without warning) if care preserving pre-1.8 behavior?
note _copyto
can imported numpy.lib.nanfunctions
numpy
No comments:
Post a Comment