-
-
Notifications
You must be signed in to change notification settings - Fork 10.8k
BUG: bugfix for #28589 (quantile should error when weights are all zeros) #28594
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
base: main
Are you sure you want to change the base?
Conversation
…eights are all zeros numpy#28589 Previously, np.quantile with all weights set to zero returned the first sample. This is fixed now via adding an explicit check that raises a ValueError when all weights are zero, preventing division by zero in the CDF. - Added the weight check in `_quantile` (line 4874 ff) to detect all-zero weights and raise an error. - Added a test in `test_function_base.py` to verify this behavior. - Added a second test to ensure existing quantile functionality remains unchanged. This should close numpy#28589
result = np.quantile(arr, quantile, weights=weights, method='inverted_cdf') | ||
expected = 2.5 | ||
|
||
assert np.isclose(result, expected) |
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.
Hopefully this part of the diff isn't really needed--presumably we have sufficient testing that if you broke the numerical behavior of quantile
an existing test would fail.
That said, this test actually does fail--maybe there is a problem with the test design?
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.
As an aside, assert_allclose()
is generally preferable in testing--it tends to give better feedback when something fails, etc.
weights = np.array([0, 0, 0, 0]) | ||
|
||
with pytest.raises(ValueError, match="All weights are zero, cannot compute quantile."): | ||
np.quantile(arr, quantile, weights=weights, method='inverted_cdf') |
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.
Note for other NumPy devs--this/this PR is very similar to gh-28597
This commit addresses the following BUG: quantile should error when weights are all zeros #28589
Previously, np.quantile with all weights set to zero returned the first sample. This is fixed now via adding an explicit check that raises a ValueError when all weights are zero, preventing division by zero in the CDF.
_quantile
(line 4874 ff) to detect all-zero weights and raise an error.test_function_base.py
to verify this behavior.This should close #28589