Numpy: .max() and .min() don't work on arrays of type 'S' and 'U' (Trac #1316)

Created on 19 Oct 2012  ·  4Comments  ·  Source: numpy/numpy

_Original ticket http://projects.scipy.org/numpy/ticket/1316 on 2009-12-07 by @mdboom, assigned to unknown._

This is a sensible operation. It would be nice to make it work if possible.

In [25]: x.max()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/wonkabar/data1/builds/betadrizzle/<ipython console> in <module>()

TypeError: cannot perform reduce with flexible type

In [26]: x.min()
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/wonkabar/data1/builds/betadrizzle/<ipython console> in <module>()

TypeError: cannot perform reduce with flexible type

In [27]:
00 - Bug Other

All 4 comments

This should be doable. Note that argmax and argmin work and might provide and easy way to implement this without using maximum.reduce.

.max can take a tuple of axes, argmax can only handle a single axis, so to get it to work for the general case is going to require a lot of axis shuffling, and possibly copying to coalesce those axes. keepdims and out are also in the signature of max but not of argmax. What types would be involved? 'S' ,'U' and 'V'?

Is this the same problem as described in nr. 9 here?

In [1]: np.array([['dd', 'de', 'cc'], ['ae', 'be', 'hf']]).max(axis=0)
TypeError: cannot perform reduce with flexible type

To @jondo 's question (nearly 4 years ago at this point but worth addressing), yes.

Dr Feldman's complaint is best expressed with an example. He says "Because NumPy's .min() and .max() methods work for numeric arrays, and Python's min() and max() functions work for strings, one might reasonably expect NumPy's .min() and .max() methods to work for arrays of strings, but they don't[...]"

Here's an illustration:

import numpy as np

arr_str = np.array(["I'm", "Defying", "Gravity"]) #A wickedly simple array of strings

print(arr_str.max()) #raises "TypeError: cannot perform reduce with flexible type"
#Interestingly, when I just used a native Python list, the error for this line was: "AttributeError: 'list' object has no attribute 'max'"

print(max(arr_str)) #does not raise any kind of error and returns "I'm" no matter where it is in the array of strings above

#This code works as expected
arr_num = np.array([1,2,3,-1])
print(max(arr_num))

Dr Feldman's issue as he stated is about arrays of strings and wanting to use .max() rather than max(); but we can expand it to a concern about any flexible type. Note that he does not mention that max() works just fine when we pass the array in as a parameter, but as you can see from the example above, it does work.

I verified what @charris said, that .argmax() does work.

My question for the community would be: is this enough of a problem (having to use either max(list) or list.argmax) that it's worth diving into the axis issues that @jaimefrio brought up?

Was this page helpful?
0 / 5 - 0 ratings