Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

BUG: quantile should error when weights are all zeros #28595

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

Open
wants to merge 19 commits into
base: main
Choose a base branch
Loading
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Modified tests and added release note
  • Loading branch information
Tontonio3 committed Mar 27, 2025
commit ae8cd54d289196ea2c4af8b7e6748a9d2cf87d89
7 changes: 7 additions & 0 deletions 7 doc/release/upcoming_changes/28589.improvement.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Improved error handling in `np.quantile`
---------------------------------
`np.quantile` now raises errors if:

* All weights are zero
* At least one weight is `np.nan`
* At least one weight is `np.inf`
30 changes: 10 additions & 20 deletions 30 numpy/lib/tests/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4151,20 +4151,16 @@ def test_inf_err(self):

for i in range(len(arr)):
wgt[i] = np.inf
try:
with pytest.raises(ValueError) as ex:
a = np.quantile(arr, q, weights=wgt, method=m)
except Exception as ex:
assert ex.__class__ == ValueError
assert str(ex) == "Weights must be non-infinite"
assert str(ex) == "Weights must be non-infinite"
wgt[i] = 1
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably sufficient to just check a single index rather than all of them. For the message check, I think we usually prefer using the match builtin to pytest.raises these days.


for i in range(len(arr)):
wgt[i] = np.inf
try:
with pytest.raises(ValueError) as ex:
a = np.quantile(arr, q, weights=wgt, method=m)
except Exception as ex:
assert ex.__class__ == ValueError
assert str(ex) == "Weights must be non-infinite"
assert str(ex) == "Weights must be non-infinite"

def test_nan_err(self):

Expand All @@ -4175,32 +4171,26 @@ def test_nan_err(self):

for i in range(len(arr)):
wgt[i] = np.nan
try:
with pytest.raises(ValueError) as ex:
a = np.quantile(arr, q, weights=wgt, method=m)
except Exception as ex:
assert ex.__class__ == ValueError
assert str(ex) == "At least one weight is nan"
assert str(ex) == "At least one weight is nan"
wgt[i] = 1

for i in range(len(arr)):
wgt[i] = np.nan
try:
with pytest.raises(ValueError) as ex:
a = np.quantile(arr, q, weights=wgt, method=m)
except Exception as ex:
assert ex.__class__ == ValueError
assert str(ex) == "At least one weight is nan"
assert str(ex) == "At least one weight is nan"

def test_all_zeroes_err(self):

m = "inverted_cdf"
q = 0.5
arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
wgt = np.zeros(10)
try:
with pytest.raises(ValueError) as ex:
a = np.quantile(arr, q, weights=wgt, method=m)
except Exception as ex:
assert ex.__class__ == ValueError
assert str(ex) == "At least one weight must be non-zero"
assert str(ex) == "At least one weight must be non-zero"


class TestLerp:
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.