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

ENH: Add dtype argument in np.choose #19364

Closed
wants to merge 3 commits into from
Closed

Conversation

ajthr
Copy link

@ajthr ajthr commented Jun 27, 2021

Fixes GH-18798

This PR adds a dtype argument to np.choose() function.

@@ -1857,7 +1857,7 @@ Item selection and manipulation

.. c:function:: PyObject* PyArray_Choose( \
PyArrayObject* self, PyObject* op, PyArrayObject* ret, \
NPY_CLIPMODE clipmode)
PyArray_Descr *dtype, NPY_CLIPMODE clipmode)
Copy link
Member

@seberg seberg Jun 28, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can't change this function, its public API and must remain ABI and API compatible. Same for PyArray_ConvertToCommonType, although you can make a private version and add a thin wrapper as public version.

I am confused about this, I thought np.choose worked with sequences, because PyArray_Choose does. But its not the case, the python code apparently only ever choose from a single array. To properly support sequences, the right thing is probably to re-use some of the concatenate logic. But since the Python code effectively always calls np.asarray() already, that might already be unnecessarily complicated.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At first, I wrote a function PyArray_ConvertToDataType. But it almost used the same logic and the code was almost identical to PyArray_ConvertToCommonType. That's why I made some changes to that existing function.
Regarding the implementation, I looked into the np.concatenate source as you mentioned in the issue. But I thought this was much cleaner than that complex way. didn't really consider the fact that it was a public API.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but PyArray_ConvertToCommonType is limited to using type-numbers. That is unfortunately not future-proof API, and we have no choice but stop using it and hope everyone follows.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will it be a good idea to create a new function for np.choose and call PyArray_Choose from the new function and changing the dtype of the returned array?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I mid-slipped there. arr.choose uses arr as the index array, not for the choices. So it all adds up, and we are firmly at the same place where concatenate also lives.

Are you working of the current branch? I added PyArray_FindConcatenationDescriptor for concatenate, which I would hope you can just use pretty straight forward.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I am working on the current branch. I will look into PyArray_FindConcatenationDescriptor.
Thank You.

@seberg seberg marked this pull request as draft June 30, 2021 23:07
@seberg seberg added the 64 - Good Idea Inactive PR with a good start or idea. Consider studying it if you are working on a related issue. label Jan 20, 2024
@seberg
Copy link
Member

seberg commented Jan 20, 2024

It is a good start, but needs work (can't chang epublic API), and as it is 2.5 years old by now closing. Please do not hesitate to make a new PR (also fo rsomeone else picking it up).

@seberg seberg closed this Jan 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
01 - Enhancement 64 - Good Idea Inactive PR with a good start or idea. Consider studying it if you are working on a related issue.
Projects
Development

Successfully merging this pull request may close these issues.

accept a dtype argument in np.choose
2 participants