Skip to content

Commit

Permalink
Backport PR #44561: CI: fix deprecation warning on interpolation (#44565
Browse files Browse the repository at this point in the history
)

Co-authored-by: Matthew Zeitlin <[email protected]>
  • Loading branch information
meeseeksmachine and mzeitlin11 authored Nov 21, 2021
1 parent 6462e54 commit f2447ae
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
6 changes: 6 additions & 0 deletions pandas/compat/numpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 5 additions & 2 deletions pandas/core/nanops.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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:
Expand Down
9 changes: 7 additions & 2 deletions pandas/tests/frame/methods/test_quantile.py
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down

0 comments on commit f2447ae

Please sign in to comment.