-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
ENH: let zeros, empty, and empty_like accept dtype classes #23913
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
Conversation
Very nice, this has been on the not done list for a while :)!
Yes, I think ideally I have two comments:
|
Done.
Ah good call, I didn't think of this API hook but it makes sense to use it here. In practice it looks like all the parametric dtypes in numpy-user-dtypes have a singleton and don't error from calling |
5991ee2
to
bb62384
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks. Will have a quick look after the weekend, but if you wish to press on, please feel free to merge yourself (I haven't carefully audited the refcounts yet, but they look good).
/* steals the reference to dtype if it's not NULL */ | ||
ret = (PyArrayObject *)PyArray_NewLikeArrayWithShape(prototype, order, dtype, | ||
shape.len, shape.ptr, subok); | ||
/* steals the reference to dt_info.descr if it's not NULL */ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do prefer no stealing, but happy to leave it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would also prefer not to steal the references, but PyArray_NewLikeArrayWithShape
is called by PyArray_NewLikeArray
, which has many consumers and I didn't want to go through all of them to make sure the references are correct.
Yes, I think the "new like" path shows that it should really be an error for our strings. Returning an S1 is weird, but returning the same thing as |
1e2380c
to
fdcc67b
Compare
Thanks Nathan, LGTM. |
Following on from #23404, this converts
np.empty
,np.empty_like
, andnp.zeros
to accept dtype classes in thedtype
argument.np.array
and similar can usePyArray_AdaptDescriptorToArray
to detect the appropriate dtype for a given input array by scanning input entries. However, I think functions likenp.empty
need a dtype instance for parametric dtypes.For non-legacy dtypes that is trivial to implement, but I wasn't clear how to handle the legacy parametric instances, which
PyArray_ExtractDTypeAndDescriptor
will discard in favor of the dtype class. That means I can't just reject legacy parametric dtype classes, so I use the singleton instead. I don't know if that's the correct thing to do.For what it's worth,
np.empty(.., dtype='U')
is probably pretty useless, and maybe we could deprecate it? For strings, you end up with aU1
dtype, which is likely not what people really want if they are working with other non-empty string arrays.Suggestions on better ways to handle legacy parametric dtypes are very welcome.