-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
ENH: make np.where a ufunc #8994
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
Love this idea! |
Yes, this would be awesome! This would only work for the three argument version of |
I believe that they are possible. However, would it be possible to make the one-argument version work with this? Or would the three-argument version defer to this? |
I'd be willing to work up a PR if we can decide on a new home for this ufunc so we support the one-argument where. |
@hameerabbasi - I think the idea would be that Questions to all: what should be the name of the ufunc? We already have p.s. On dealing with void and string: that is a bit trickier, as it needs passing on lengths, etc. Possibly it is best to just start with the regular dtypes... |
In the issue I opened (before opening this one), I suggested
Use >>> np.promote_types('S8', 'S11')
dtype('S11')
>>> dt1 = np.dtype([('f1', np.int16), ('f2', np.float32)])
>>> dt2 = np.dtype([('f5', np.int16), ('f6', np.float32)])
>>> np.promote_types(dt1, dt2)
Traceback (most recent call last):
File "<input>", line 1, in <module>
TypeError: invalid type promotion
>>> np.promote_types(dt1, dt1)
dtype([('f1', '<i2'), ('f2', '<f4')]) |
I don't like |
I like the name np.conditional. "merge" suggests something database like.
"cond" matches the name from lisp, but we don't need a short name when we
will still have "where"
…On Sat, Feb 24, 2018 at 10:42 AM Hameer Abbasi ***@***.***> wrote:
Questions to all: what should be the name of the ufunc? We already have
np.select. Harking to c, perhaps np.conditional(condition, a, b). Or, more
fortran-ish, np.merge(a, b, condition)? Or should it be a private function
that only gets called by np.where?
In the issue I opened (before opening this one), I suggested if, but it's
a Python keyword so not optimal. trinary would also work since it mimics
the trinary operator. ifx from LaTeX would make sense too.
p.s. On dealing with void and string: that is a bit trickier, as it needs
passing on lengths, etc. Possibly it is best to just start with the regular
dtypes...
Use np.promote_types? Doesn't work for all cases though.
>>> np.promote_types('S8', 'S11')
dtype('S11')>>> dt1 = np.dtype([('f1', np.int16), ('f2', np.float32)])>>> dt2 = np.dtype([('f5', np.int16), ('f6', np.float32)])>>> np.promote_types(dt1, dt2)
Traceback (most recent call last):
File "<input>", line 1, in <module>TypeError: invalid type promotion>>> np.promote_types(dt1, dt1)
dtype([('f1', '<i2'), ('f2', '<f4')])
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#8994 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ABKS1mEAqEz1N7-odyYQiIWBsHRVxVueks5tYFf7gaJpZM4NJF4A>
.
|
I also had another idea. I know numpy doesn't follow this too strictly, but how about |
I'm +1 on the name |
The casting would, I think, be fairly similar to addition (except that of course the boolean does not influence the outcome). The regular ufuncs are all defined in |
Regarding naming, I think |
Should be very geared towards a
bXX->X
loop, for every possibleX
.This would offer:
out
argument__array_ufunc__
where
argument (!) such thatnp.where(c, y, z, out=x, where=w)
is a more efficientx = np.where(w, x, np.where(c, y, z))
Problems:
void
and other flexible types possible?The text was updated successfully, but these errors were encountered: