-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
ENH: Uniform interface for accessing minimum or maximum value of a dtype #27785
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
Comments
Thanks for posting! My personal favourite would be It also might be simpler for an initial implementation, since it will be easy to wrap the existing code. p.s. While I think something on the dtype might be cleanest, it is of course possible to think of something like a registration system that would allow something like |
Yes, it would be nice to have this. Have to carefully think about a 1:1 mapping with iinfo/finfo, though (i.e. return an iinfo/finfo instance). The reason is that |
@seberg Did you mean You raise a good point that For precisely the use case you described, I'm interested in extracting the actual min/max value, that is, the identity element for max/min, respectively. |
Improving user-defined dtypes seems like an important need, as pointed out on the mailing list. That should apply to the current The commonality of the user-defined dtypes topic with this feature request is that it requires stashing metadata on the dtype somewhere (either publicly or privately). I'll note that these are all links to you asking for this same feature. What I am missing is a more compelling use case, it would be great to add one (a The other point is that semantically the >>> np.finfo(np.float64).max * 2 # returns a NumPy scalar
<ipython-input-12-b98731d4261a>:1: RuntimeWarning: overflow encountered in scalar multiply
np.finfo(np.float64).max * 2
np.float64(inf)
>>> np.iinfo(np.int64).max * 2 # returns a Python int
18446744073709551614
>>> fmax = np.finfo(np.float64).max
>>> imax = np.iinfo(np.int64).max
>>> fmax + 1 == fmax
np.True_
>>> imax + 1 == imax
False Which is probably why it's hard to find code that actually needs this. Re standalone function vs dtype method/attribute: if you go for The discussion on the mailing list asked for a NEP - that seems justified indeed. |
Why not? It seems like a perfectly good use case: One wants the sum/product/max/min of an array that happens to be empty (either by size or masking) to be the identity element for sum/product/max/min, respectively. |
FWIW, The important part is that any such information must be well defined, and I think if you do that the typing will be a nice check, but will just fall into place. An interesting thing to keep in mind is also complex numbers, Having a single name entry-point seems more convenient, but of course it means that the all attributes must be very well defined, e.g.:
Besides being very inconvenient and slow, for floats you can infer almost all interesting values (an interesting problem is complex numbers, but I suspect one can deal with it). The minimum value problem may be one of the few more used things you can't infer right now for non-floats (for floats
It's a good use, but it is helpful to mention the actual code/use-case where you need to do this for a mix of float/non-float for example. |
Yes this☝🏼. What's the context, what are you trying to achieve and why? And why you need it often enough that the one-liner you've already received is too annoying? And do you find this code pattern, or similar such patterns, in other code: >>> def min_val(dtype):
... return (False if dtype == bool else np.iinfo(dtype).min if np.issubdtype(dtype, np.integer)
... else np.finfo(dtype).min)
...
>>> arr = np.array([])
>>> np.max(arr, initial=min_val(arr.dtype))
np.float64(-1.7976931348623157e+308) Basically, this doesn't allow anything new yet, it just saves a bit of typing for what looks like a fairly niche use case (niche because normally you'd want either an exception or an |
Uh oh!
There was an error while loading. Please reload this page.
Proposed new feature or change:
Mailing list post
Feature request: Add a uniform interface for accessing the minimum or maximum value of a given dtype.
Previously discussed here, here, and here. Currently, doing this requires branching on the type of dtype (boolean, integer, or floating point) and then (for the latter two) calling either iinfo or finfo, respectively. It would be more ergonomic to have a single, uniform interface for accessing this information that is dtype-independent.
Here are the possible interfaces suggested so far:
Personally, my current favorite is simply
dt.min
.Relevant comment by @mhvk on the mailing list thread:
The text was updated successfully, but these errors were encountered: