Skip to content

Commit 8932173

Browse files
ERR: Improve error message in validate_percentile (#51475)
1 parent 9271d25 commit 8932173

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

pandas/tests/series/methods/test_quantile.py

+5
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,11 @@ def test_quantile(self, datetime_series):
4343
with pytest.raises(ValueError, match=msg):
4444
datetime_series.quantile(invalid)
4545

46+
s = Series(np.random.randn(100))
47+
percentile_array = [-0.5, 0.25, 1.5]
48+
with pytest.raises(ValueError, match=msg):
49+
s.quantile(percentile_array)
50+
4651
def test_quantile_multi(self, datetime_series):
4752
qs = [0.1, 0.9]
4853
result = datetime_series.quantile(qs)

pandas/util/_validators.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -329,12 +329,13 @@ def validate_percentile(q: float | Iterable[float]) -> np.ndarray:
329329
q_arr = np.asarray(q)
330330
# Don't change this to an f-string. The string formatting
331331
# is too expensive for cases where we don't need it.
332-
msg = "percentiles should all be in the interval [0, 1]. Try {} instead."
332+
msg = "percentiles should all be in the interval [0, 1]"
333333
if q_arr.ndim == 0:
334334
if not 0 <= q_arr <= 1:
335-
raise ValueError(msg.format(q_arr / 100.0))
336-
elif not all(0 <= qs <= 1 for qs in q_arr):
337-
raise ValueError(msg.format(q_arr / 100.0))
335+
raise ValueError(msg)
336+
else:
337+
if not all(0 <= qs <= 1 for qs in q_arr):
338+
raise ValueError(msg)
338339
return q_arr
339340

340341

0 commit comments

Comments
 (0)