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.