Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
thouis opened this issue Oct 19, 2012 · 5 comments
Open
Labels
00 - Bug component: Other Project Possible project, may require specific skills and long commitment

Comments

@thouis
Copy link
Contributor

thouis commented Oct 19, 2012

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]:
@charris
Copy link
Member

charris commented Feb 18, 2014

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

@jaimefrio
Copy link
Member

.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'?

Prastaruszek added a commit to Prastaruszek/numpy that referenced this issue Feb 23, 2015
…s suggested

in issue numpy#1914 .

I've added function _String_Min_Or_Max to methods.c and updated functions
array_min and array_max to handle string ndarrays. One can launch this
functions with arguments - keepdims, out and axis (which can be a tuple
to enable multidimensional reductions). The solution is based
on PyArray_ArgMin and PyArray_Argmax functs, as suggested in issue.

I've also created class TestMax in core/tests/test_multidimensional.py
in which several tests are placed.
@jondo
Copy link

jondo commented Oct 16, 2015

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

@mattip mattip removed this from the NumPy 2.0 milestone Oct 21, 2018
@JessiDG
Copy link

JessiDG commented Mar 23, 2019

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?

@seberg
Copy link
Member

seberg commented May 12, 2022

This is now possible a plausible project by basing the work on gh-21041.

@seberg seberg added the Project Possible project, may require specific skills and long commitment label May 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
00 - Bug component: Other Project Possible project, may require specific skills and long commitment
Projects
None yet
Development

No branches or pull requests

7 participants