BUG: fix ma.median dtype promotion for float16/float32 inputs#31560
Open
Atharva2012 wants to merge 1 commit into
Open
BUG: fix ma.median dtype promotion for float16/float32 inputs#31560Atharva2012 wants to merge 1 commit into
Atharva2012 wants to merge 1 commit into
Conversation
Author
|
@seberg This is my first PR for this project. Could use your opinion. |
Member
|
Sorry, but this is the wrong approach. You should make sure that |
8c4d18f to
ec80318
Compare
Author
|
@seberg Thank you for the feedback! After checking, both np.median and np.ma.median return the same dtype as the input consistently, so this is a documentation issue. I've updated the docstring to accurately reflect the actual behavior. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR summary
np.ma.median does not promote float16 and float32 inputs to float64 as documented. The docstring states the return type should be float64 for floats smaller than float64, but the result stays in the input dtype instead.
The bug is in _median in numpy/ma/extras.py. Two code paths, the 1D path and the N-D path, use np.true_divide without first casting to the correct output dtype. The fix computes out_dtype = np.result_type(asorted.dtype, np.float64) and casts before dividing in both paths.
UPDATE: After reviewer feedback, it was found that both np.median and np.ma.median return the same dtype as the input consistently. The issue is in the docstring, not the code. This PR now corrects the docstring in numpy/ma/extras.py to accurately reflect the actual behavior.
Closes #31486 Related to: #29003
First time committer introduction
I am Atharva, I use NumPy for academic projects, but it's my first time contributing to this open source project. I kept the change small and focused on a single well-defined bug.
** AI Disclosure**
AI was used to reword the PR description in more suitable language.