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
Changed infinite error message
  • Loading branch information
Tontonio3 committed Mar 28, 2025
commit bfcec095a681646491c5640f1c852247c8e7e373
2 changes: 1 addition & 1 deletion 2 numpy/lib/_function_base_impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -4537,7 +4537,7 @@ def quantile(a,
weights = _weights_are_valid(weights=weights, a=a, axis=axis)
if weights.dtype != object:
Copy link
Contributor

Choose a reason for hiding this comment

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

A general comment: I think these checks should happen inside _weights_ar_valid - this will ensure they are used for percentile as well.

if np.any(np.isinf(weights)):
Copy link
Contributor

Choose a reason for hiding this comment

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

Another general comment: as written, the common case has to go through a lot of checks. I think it would be better to optimize for the common case, and not worry too much about distinguishing failure cases. E.g., you can do just one evaluation with:

if not np.all(np.isfinite(weights)):
    raise ValueError("weights must be finite.")

raise ValueError("Weights must be non-infinite.")
raise ValueError("Weights must be finite.")
elif np.any(np.isnan(weights)):
raise ValueError("At least one weight is nan.")
# Since np.isinf and np.isnan do not work in dtype object arrays
Expand Down
10 changes: 5 additions & 5 deletions 10 numpy/lib/tests/test_function_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -4144,11 +4144,11 @@ def test_closest_observation(self):


@pytest.mark.parametrize(["err_msg", "weight"],
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd parametrize over np.quantile and np.percentile as well - they should have the same errors.

Copy link
Contributor

Choose a reason for hiding this comment

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

Also, if you pass in a list rather than an array, you could parametrize over dtype=float and dtype=object, to make this a little more readable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

[("Weights must be non-infinite.", np.array([1,np.inf, 1, 1])),
("Weights must be non-infinite.", np.array([1,np.inf, 1, 1], dtype=object)),
("Weights must be non-infinite.", np.array([1,-np.inf, 1, 1])),
("Weights must be non-infinite.", np.array([1,-np.inf, 1, 1], dtype=object)),
("Weights must be non-infinite.", np.array([1,np.inf, 1, np.inf])),
[("Weights must be finite.", np.array([1,np.inf, 1, 1])),
("Weights must be finite.", np.array([1,np.inf, 1, 1], dtype=object)),
("Weights must be finite.", np.array([1,-np.inf, 1, 1])),
("Weights must be finite.", np.array([1,-np.inf, 1, 1], dtype=object)),
("Weights must be finite.", np.array([1,np.inf, 1, np.inf])),
("At least one weight is nan.", np.array([1,np.nan, 1, 1])),
("At least one weight is nan.", np.array([1,np.nan, 1, 1], dtype=object)),
("At least one weight is nan.", np.array([1,np.nan, np.nan, 1])),
Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.