diff --git a/pandas/compat/numpy/__init__.py b/pandas/compat/numpy/__init__.py index 619713f28ee2d..2d60164ccf075 100644 --- a/pandas/compat/numpy/__init__.py +++ b/pandas/compat/numpy/__init__.py @@ -12,9 +12,15 @@ np_version_under1p18 = _nlv < Version("1.18") np_version_under1p19 = _nlv < Version("1.19") np_version_under1p20 = _nlv < Version("1.20") +np_version_under1p22 = _nlv < Version("1.22") is_numpy_dev = _nlv.dev is not None _min_numpy_ver = "1.17.3" +if is_numpy_dev or not np_version_under1p22: + np_percentile_argname = "method" +else: + np_percentile_argname = "interpolation" + if _nlv < Version(_min_numpy_ver): raise ImportError( diff --git a/pandas/core/nanops.py b/pandas/core/nanops.py index 3b03a28afe163..aabe03586385b 100644 --- a/pandas/core/nanops.py +++ b/pandas/core/nanops.py @@ -29,6 +29,7 @@ Shape, ) from pandas.compat._optional import import_optional_dependency +from pandas.compat.numpy import np_percentile_argname from pandas.core.dtypes.common import ( get_dtype, @@ -1667,7 +1668,7 @@ def _nanpercentile_1d( if len(values) == 0: return np.array([na_value] * len(q), dtype=values.dtype) - return np.percentile(values, q, interpolation=interpolation) + return np.percentile(values, q, **{np_percentile_argname: interpolation}) def nanpercentile( @@ -1720,7 +1721,9 @@ def nanpercentile( result = np.array(result, dtype=values.dtype, copy=False).T return result else: - return np.percentile(values, q, axis=1, interpolation=interpolation) + return np.percentile( + values, q, axis=1, **{np_percentile_argname: interpolation} + ) def na_accum_func(values: ArrayLike, accum_func, *, skipna: bool) -> ArrayLike: diff --git a/pandas/tests/frame/methods/test_quantile.py b/pandas/tests/frame/methods/test_quantile.py index f341014110e18..67591c8532f43 100644 --- a/pandas/tests/frame/methods/test_quantile.py +++ b/pandas/tests/frame/methods/test_quantile.py @@ -1,6 +1,8 @@ import numpy as np import pytest +from pandas.compat.numpy import np_percentile_argname + import pandas as pd from pandas import ( DataFrame, @@ -153,7 +155,10 @@ def test_quantile_interpolation(self): # cross-check interpolation=nearest results in original dtype exp = np.percentile( - np.array([[1, 2, 3], [2, 3, 4]]), 0.5, axis=0, interpolation="nearest" + np.array([[1, 2, 3], [2, 3, 4]]), + 0.5, + axis=0, + **{np_percentile_argname: "nearest"}, ) expected = Series(exp, index=[1, 2, 3], name=0.5, dtype="int64") tm.assert_series_equal(result, expected) @@ -167,7 +172,7 @@ def test_quantile_interpolation(self): np.array([[1.0, 2.0, 3.0], [2.0, 3.0, 4.0]]), 0.5, axis=0, - interpolation="nearest", + **{np_percentile_argname: "nearest"}, ) expected = Series(exp, index=[1, 2, 3], name=0.5, dtype="float64") tm.assert_series_equal(result, expected)