[Numpy-discussion] numpy.nansum() behavior in 1.3.0

josef.pktd@gmai... josef.pktd@gmai...
Mon Jun 1 20:28:19 CDT 2009


On Mon, Jun 1, 2009 at 9:15 PM, Robert Kern <robert.kern@gmail.com> wrote:
> On Mon, Jun 1, 2009 at 20:09, Charles R Harris
> <charlesr.harris@gmail.com> wrote:
>>
>> On Mon, Jun 1, 2009 at 6:30 PM, Robert Kern <robert.kern@gmail.com> wrote:
>>>
>>> On Mon, Jun 1, 2009 at 18:50,  <josef.pktd@gmail.com> wrote:
>>> > On Mon, Jun 1, 2009 at 7:43 PM,  <josef.pktd@gmail.com> wrote:
>>>
>>> >> is np.size the right check for non-empty array, including subtypes?
>>>
>>> Yes.
>>>
>>> >> i.e.
>>> >>
>>> >> if y.size and mask.all():
>>> >>        return np.nan
>>> >>
>>> >> or more explicit
>>> >> if y.size > 0 and mask.all():
>>> >>        return np.nan
>>> >>
>>> >
>>> > Actually, now I think this is the wrong behavior, nansum should never
>>> > return nan.
>>> >
>>> >>>> np.nansum([np.nan, np.nan])
>>> > 1.#QNAN
>>> >
>>> > shouldn't this be zero
>>>
>>> I agree.
>>
>> Would anyone be interested in ufuncs fadd/fsub that treated nans like zeros?
>> Note the fmax.reduce can be used the implement nanmax.
>
> Just please don't call them fadd/fsub. The fmin and fmax names came
> from C99. The fact that they ignore NaNs has nothing to do with the
> naming; that's just the way C99 designed those particular functions.
> Better, to my mind, would be to make a new module with NaN-ignoring
> (or maybe just -aware) semantics. The ufuncs would then be named
> add/subtract/etc.
>

my proposed fix for nansum also affects nanmax and similar,

Is there a clear definition what the result should be for empty or all
nan arrays?

Josef


>>> np.min([])
Traceback (most recent call last):
...
ValueError: zero-size array to ufunc.reduce without identity
>>> np.nanmin(np.nan)
1.#QNAN
>>> nanmin(np.nan)  # after changes to _nanop
inf
>>> np.nanargmin(np.nan)
1.#QNAN
>>> nanargmin(np.nan)  # after changes to _nanop
0
>>> nanmax(np.nan)  # after changes to _nanop
inf
>>> np.nanmax(np.nan)
1.#QNAN
>>> nanmin([])  # after changes to _nanop
Traceback (most recent call last):
...
ValueError: zero-size array to ufunc.reduce without identity
>>> np.nanmin([])
1.#QNAN


>>> np.fmax.reduce(np.array(5))
Traceback (most recent call last):
  File "<pyshell#124>", line 1, in <module>
    np.fmax.reduce(np.array(5))
TypeError: cannot reduce on a scalar
>>> np.fmax.reduce([np.nan])
nan


More information about the Numpy-discussion mailing list