14

I get a case that when I tried to use np.max() in an empty numpy array it will report such error messages.

# values is an empty numpy array here
max_val = np.max(values)

ValueError: zero-size array to reduction operation maximum which has no identity

So the way I think to fix it is that I try to deal with the empty numpy array first before calling the np.max() like follows:

# add some values as missing values on purposes.
def deal_empty_np_array(a:np.array):
    if a.size == 0:
        a = np.append(a, [-999999, -999999])

    return a

values = deal_empty_np_array(values)
max_val = np.max(values);

OR use the try catch way like this link.

So I am wondering if there is a better solution for this awkward case.
Thanks in advance.

PS: Sorry for not giving a clean description before.

Bowen Peng
  • 1,635
  • 4
  • 21
  • 39
  • for me your code doesn't make that much sense, since 1st: you seem to assign to some named column (pandas dataframe?), but `time_diff` will be a numpy array. 2nd, you never specified `time_dif_rat`, what is this? – Nico Albers Jan 18 '20 at 14:36
  • 1
    @NicoAlbers Albers Sorry, this code has a mistake. I have corrected it in the running place before but posted a wrong version. – Bowen Peng Jan 18 '20 at 15:13
  • 1
    Can you give an Minimum working (or not working) example? It seems you still mess up what `time_diff` is - an array or an dataframe? – Nico Albers Jan 18 '20 at 15:15
  • I'm not sure what the issue is here, exactly. Yes, calling `numpy.max()` on an empty array throws an error. Just check if the array is empty, if it is don't call `max()` on it. – AMC Jan 18 '20 at 17:31

2 Answers2

22
In [3]: np.max([])                                                                               
---------------------------------------------------------------------------
...
ValueError: zero-size array to reduction operation maximum which has no identity

But check the docs. In newer numpy ufunc like max take an initial parameter that lets you work with an empty array:

In [4]: np.max([],initial=10)                                                                    
Out[4]: 10.0
hpaulj
  • 221,503
  • 14
  • 230
  • 353
  • Awesome. What is a suitable way to decide on the value of initial? – jtlz2 Feb 04 '21 at 13:16
  • @jtlz2 I bet there is no less generic way of putting it than "thinking about which value you want the function to return if the input array is empty." It depends on your application. – bers Mar 09 '21 at 10:06
  • 4
    Beware that the initial value has not the same meaning as a `default` arg in Python semantics. The answer could IMO clarify that better, as the comments are clearly misleading. An initial value is included in maximum search, thus `np.max(arr, initial)` is equivalent to `np.max(np.append(arr, initial))`. For example, `np.max(np.random.rand(3), initial=10)` will return 10, which may or may be not correct, depending on your context. – hoefling Mar 26 '21 at 16:09
1

I think you can simply check it, and eventually re-assign it, before calling np.max:

import numpy as np

values = -999 if values.size==0 else values
max_val = np.max(values)
FBruzzesi
  • 6,385
  • 3
  • 15
  • 37