From 1dbd18016ce355a1feef40a4335dd1284e6442b9 Mon Sep 17 00:00:00 2001 From: ido gal Date: Thu, 27 Mar 2025 16:00:31 -0400 Subject: [PATCH] fix quantile all zeros error --- numpy/lib/_function_base_impl.py | 2 ++ numpy/lib/tests/test_function_base.py | 7 +++++++ 2 files changed, 9 insertions(+) diff --git a/numpy/lib/_function_base_impl.py b/numpy/lib/_function_base_impl.py index 73fc29db16a2..07bfa2d0b8b5 100644 --- a/numpy/lib/_function_base_impl.py +++ b/numpy/lib/_function_base_impl.py @@ -4537,6 +4537,8 @@ def quantile(a, weights = _weights_are_valid(weights=weights, a=a, axis=axis) if np.any(weights < 0): raise ValueError("Weights must be non-negative.") + if np.all(weights == 0): + raise ValueError("Weights must contain non-zero value.") return _quantile_unchecked( a, q, axis, out, overwrite_input, method, keepdims, weights) diff --git a/numpy/lib/tests/test_function_base.py b/numpy/lib/tests/test_function_base.py index e6efc4cf0a32..48d5b27c82a4 100644 --- a/numpy/lib/tests/test_function_base.py +++ b/numpy/lib/tests/test_function_base.py @@ -4110,6 +4110,13 @@ def test_quantile_weights_raises_negative_weights(self): with pytest.raises(ValueError, match="Weights must be non-negative"): np.quantile(y, 0.5, weights=w, method="inverted_cdf") + def test_quantile_weights_raises_all_zeros_weights(self): + y = [1, 2] + w = [0, 0] + with pytest.raises(ValueError, match="Weights must contain non-zero value."): + np.quantile(y, 0.5, weights=w, method="inverted_cdf") + + @pytest.mark.parametrize( "method", sorted(set(quantile_methods) - set(methods_supporting_weights)),