Skip to content

Commit 2d122fd

Browse files
committed
Fix create_violin() numpy 2.0+ compatibility
np.percentile() renamed its `interpolation` kwarg to `method` in numpy 1.22, and the old name was removed in numpy 2.0. This caused create_violin() to raise a TypeError on numpy >= 2.0. Use a version check to pick the right kwarg name so the function works on both old and new numpy. Fixes #5461
1 parent 380e0ad commit 2d122fd

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

plotly/figure_factory/_violin.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,16 @@ def calc_stats(data):
1717
x = np.asarray(data, float)
1818
vals_min = np.min(x)
1919
vals_max = np.max(x)
20-
q2 = np.percentile(x, 50, interpolation="linear")
21-
q1 = np.percentile(x, 25, interpolation="lower")
22-
q3 = np.percentile(x, 75, interpolation="higher")
20+
# np.percentile's `interpolation` kwarg was renamed to `method`
21+
# in numpy 1.22 and removed in numpy 2.0.
22+
_percentile_kwarg = (
23+
"method"
24+
if tuple(int(v) for v in np.__version__.split(".")[:2]) >= (1, 22)
25+
else "interpolation"
26+
)
27+
q2 = np.percentile(x, 50, **{_percentile_kwarg: "linear"})
28+
q1 = np.percentile(x, 25, **{_percentile_kwarg: "lower"})
29+
q3 = np.percentile(x, 75, **{_percentile_kwarg: "higher"})
2330
iqr = q3 - q1
2431
whisker_dist = 1.5 * iqr
2532

0 commit comments

Comments
 (0)