Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

quantile: rename interpolation arg to method #6108

Merged
merged 18 commits into from
Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ Breaking changes
wrapping the text once the maximum display width has been exceeded. (:issue: `5546`, :pull:`5662`)
By `Jimmy Westling <https://github.com/illviljan>`_.

- Renamed the ``interpolation`` keyword of all ``quantile`` methods (e.g. :py:meth:`DataArray.quantile`)
to ``method`` for consistency with numpy v1.22.0 (:pull:`6108`).
By `Mathias Hauser <https://github.com/mathause>`_.

Deprecations
~~~~~~~~~~~~
Expand Down
28 changes: 13 additions & 15 deletions xarray/core/dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
from .indexes import Index, Indexes, default_indexes, propagate_indexes
from .indexing import is_fancy_indexer
from .merge import PANDAS_TYPES, MergeError, _extract_indexes_from_coords
from .npcompat import ArrayLike
from .options import OPTIONS, _get_keep_attrs
from .utils import (
Default,
Expand Down Expand Up @@ -3433,11 +3434,12 @@ def sortby(

def quantile(
self,
q: Any,
dim: Union[Hashable, Sequence[Hashable], None] = None,
interpolation: str = "linear",
q: ArrayLike,
dim: Union[str, Sequence[Hashable], None] = None,
method: str = "linear",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can use Literal["linear", etc] here instead of str.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel that's a bit over the top - can I get away without it?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree it would be cool, definitely fine without out too! :)

keep_attrs: bool = None,
skipna: bool = True,
interpolation: str = None,
) -> "DataArray":
"""Compute the qth quantile of the data along the specified dimension.

Expand All @@ -3449,18 +3451,13 @@ def quantile(
Quantile to compute, which must be between 0 and 1 inclusive.
dim : hashable or sequence of hashable, optional
Dimension(s) over which to apply quantile.
interpolation : {"linear", "lower", "higher", "midpoint", "nearest"}, default: "linear"
dcherian marked this conversation as resolved.
Show resolved Hide resolved
method : str, default: "linear"
This optional parameter specifies the interpolation method to
use when the desired quantile lies between two data points
``i < j``:

- linear: ``i + (j - i) * fraction``, where ``fraction`` is
the fractional part of the index surrounded by ``i`` and
``j``.
- lower: ``i``.
- higher: ``j``.
- nearest: ``i`` or ``j``, whichever is nearest.
- midpoint: ``(i + j) / 2``.
use when the desired quantile lies between two data points.
See numpy.quantile for available methods.
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this ok or should we list them explicitly? (The available methods depend on the numpy version which makes this a bit difficult).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would lean toward listing the current available methods but if not, we should make sure numpy.quantile get's picked up as a link by intersphinx.


This argument was previously called "interpolation", renamed in accordance
with numpy version 1.22.0.
keep_attrs : bool, optional
If True, the dataset's attributes (`attrs`) will be copied from
the original object to the new one. If False (default), the new
Expand Down Expand Up @@ -3518,8 +3515,9 @@ def quantile(
q,
dim=dim,
keep_attrs=keep_attrs,
interpolation=interpolation,
method=method,
skipna=skipna,
interpolation=interpolation,
)
return self._from_temp_dataset(ds)

Expand Down
56 changes: 31 additions & 25 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
merge_data_and_coords,
)
from .missing import get_clean_interp_index
from .npcompat import ArrayLike
from .options import OPTIONS, _get_keep_attrs
from .pycompat import is_duck_dask_array, sparse_array_type
from .utils import (
Expand Down Expand Up @@ -6152,12 +6153,13 @@ def sortby(self, variables, ascending=True):

def quantile(
self,
q,
dim=None,
interpolation="linear",
numeric_only=False,
keep_attrs=None,
skipna=True,
q: ArrayLike,
dim: Union[str, Iterable[Hashable]] = None,
method: str = "linear",
numeric_only: bool = False,
keep_attrs: bool = None,
skipna: bool = True,
interpolation: str = None,
):
"""Compute the qth quantile of the data along the specified dimension.

Expand All @@ -6170,18 +6172,13 @@ def quantile(
Quantile to compute, which must be between 0 and 1 inclusive.
dim : str or sequence of str, optional
Dimension(s) over which to apply quantile.
interpolation : {"linear", "lower", "higher", "midpoint", "nearest"}, default: "linear"
method : str, default: "linear"
This optional parameter specifies the interpolation method to
use when the desired quantile lies between two data points
``i < j``:

* linear: ``i + (j - i) * fraction``, where ``fraction`` is
the fractional part of the index surrounded by ``i`` and
``j``.
* lower: ``i``.
* higher: ``j``.
* nearest: ``i`` or ``j``, whichever is nearest.
* midpoint: ``(i + j) / 2``.
use when the desired quantile lies between two data points.
See numpy.quantile for available methods.

This argument was previously called "interpolation", renamed in accordance
with numpy version 1.22.0.
keep_attrs : bool, optional
If True, the dataset's attributes (`attrs`) will be copied from
the original object to the new one. If False (default), the new
Expand Down Expand Up @@ -6242,15 +6239,29 @@ def quantile(
a (quantile, y) float64 0.7 4.2 2.6 1.5 3.6 ... 1.7 6.5 7.3 9.4 1.9
"""

# interpolation renamed to method in version 0.21.0
# check here and in variable to avoid repeated warnings
if interpolation is not None:
warnings.warn(
"The `interpolation` argument to quantile was renamed to `method`.",
FutureWarning,
)

if method != "linear":
raise TypeError("Cannot pass interpolation and method keywords!")

method = interpolation

dims: set[Hashable]
if isinstance(dim, str):
dims = {dim}
elif dim in [None, ...]:
elif dim is None or dim is ...:
dims = set(self.dims)
else:
dims = set(dim)

_assert_empty(
[d for d in dims if d not in self.dims],
tuple(d for d in dims if d not in self.dims),
"Dataset does not contain the dimensions: %s",
)

Expand All @@ -6266,15 +6277,10 @@ def quantile(
or np.issubdtype(var.dtype, np.number)
or var.dtype == np.bool_
):
if len(reduce_dims) == var.ndim:
# prefer to aggregate over axis=None rather than
# axis=(0, 1) if they will be equivalent, because
# the former is often more efficient
reduce_dims = None
variables[name] = var.quantile(
q,
dim=reduce_dims,
interpolation=interpolation,
method=method,
keep_attrs=keep_attrs,
skipna=skipna,
)
Expand Down
28 changes: 15 additions & 13 deletions xarray/core/groupby.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,7 +549,13 @@ def fillna(self, value):
return ops.fillna(self, value)

def quantile(
self, q, dim=None, interpolation="linear", keep_attrs=None, skipna=True
self,
q,
dim=None,
method="linear",
keep_attrs=None,
skipna=True,
interpolation=None,
):
"""Compute the qth quantile over each array in the groups and
concatenate them together into a new array.
Expand All @@ -562,18 +568,13 @@ def quantile(
dim : ..., str or sequence of str, optional
Dimension(s) over which to apply quantile.
Defaults to the grouped dimension.
interpolation : {"linear", "lower", "higher", "midpoint", "nearest"}, default: "linear"
method : str, default: "linear"
This optional parameter specifies the interpolation method to
use when the desired quantile lies between two data points
``i < j``:

* linear: ``i + (j - i) * fraction``, where ``fraction`` is
the fractional part of the index surrounded by ``i`` and
``j``.
* lower: ``i``.
* higher: ``j``.
* nearest: ``i`` or ``j``, whichever is nearest.
* midpoint: ``(i + j) / 2``.
use when the desired quantile lies between two data points.
See numpy.quantile for available methods.

This argument was previously called "interpolation", renamed in accordance
with numpy version 1.22.0.
skipna : bool, optional
Whether to skip missing values when aggregating.

Expand Down Expand Up @@ -648,9 +649,10 @@ def quantile(
shortcut=False,
q=q,
dim=dim,
interpolation=interpolation,
method=method,
keep_attrs=keep_attrs,
skipna=skipna,
interpolation=interpolation,
)
return out

Expand Down
49 changes: 35 additions & 14 deletions xarray/core/variable.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import numpy as np
import pandas as pd
from packaging.version import Version

import xarray as xr # only for Dataset and DataArray

Expand All @@ -35,6 +36,7 @@
VectorizedIndexer,
as_indexable,
)
from .npcompat import ArrayLike
from .options import OPTIONS, _get_keep_attrs
from .pycompat import (
DuckArrayModule,
Expand Down Expand Up @@ -1980,8 +1982,14 @@ def no_conflicts(self, other, equiv=duck_array_ops.array_notnull_equiv):
return self.broadcast_equals(other, equiv=equiv)

def quantile(
self, q, dim=None, interpolation="linear", keep_attrs=None, skipna=True
):
self,
q: ArrayLike,
dim: Union[str, Sequence[Hashable]] = None,
method: str = "linear",
keep_attrs: bool = None,
skipna: bool = True,
interpolation: str = None,
) -> "Variable":
"""Compute the qth quantile of the data along the specified dimension.

Returns the qth quantiles(s) of the array elements.
Expand All @@ -1993,18 +2001,14 @@ def quantile(
inclusive.
dim : str or sequence of str, optional
Dimension(s) over which to apply quantile.
interpolation : {"linear", "lower", "higher", "midpoint", "nearest"}, default: "linear"
method : str, default: "linear"
This optional parameter specifies the interpolation method to
use when the desired quantile lies between two data points
``i < j``:

* linear: ``i + (j - i) * fraction``, where ``fraction`` is
the fractional part of the index surrounded by ``i`` and
``j``.
* lower: ``i``.
* higher: ``j``.
* nearest: ``i`` or ``j``, whichever is nearest.
* midpoint: ``(i + j) / 2``.
use when the desired quantile lies between two data points.
See numpy.quantile for available methods.

This argument was previously called "interpolation", renamed in accordance
with numpy version 1.22.0.

keep_attrs : bool, optional
If True, the variable's attributes (`attrs`) will be copied from
the original object to the new one. If False (default), the new
Expand All @@ -2027,6 +2031,17 @@ def quantile(

from .computation import apply_ufunc

if interpolation is not None:
warnings.warn(
"The `interpolation` argument to quantile was renamed to `method`.",
FutureWarning,
)

if method != "linear":
raise TypeError("Cannot pass interpolation and method keywords!")

method = interpolation

_quantile_func = np.nanquantile if skipna else np.quantile

if keep_attrs is None:
Expand All @@ -2046,6 +2061,12 @@ def _wrapper(npa, **kwargs):
return np.moveaxis(_quantile_func(npa, **kwargs), 0, -1)

axis = np.arange(-1, -1 * len(dim) - 1, -1)

if Version(np.__version__) >= Version("1.22.0"):
kwargs = {"q": q, "axis": axis, "method": method}
else:
kwargs = {"q": q, "axis": axis, "interpolation": method}

result = apply_ufunc(
_wrapper,
self,
Expand All @@ -2055,7 +2076,7 @@ def _wrapper(npa, **kwargs):
output_dtypes=[np.float64],
dask_gufunc_kwargs=dict(output_sizes={"quantile": len(q)}),
dask="parallelized",
kwargs={"q": q, "axis": axis, "interpolation": interpolation},
kwargs=kwargs,
)

# for backward compatibility
Expand Down
34 changes: 33 additions & 1 deletion xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2527,7 +2527,7 @@ def test_reduce_out(self):
@pytest.mark.parametrize(
"axis, dim", zip([None, 0, [0], [0, 1]], [None, "x", ["x"], ["x", "y"]])
)
def test_quantile(self, q, axis, dim, skipna):
def test_quantile(self, q, axis, dim, skipna) -> None:
actual = DataArray(self.va).quantile(q, dim=dim, keep_attrs=True, skipna=skipna)
_percentile_func = np.nanpercentile if skipna else np.percentile
expected = _percentile_func(self.dv.values, np.array(q) * 100, axis=axis)
Expand All @@ -2539,6 +2539,38 @@ def test_quantile(self, q, axis, dim, skipna):

assert actual.attrs == self.attrs

@pytest.mark.parametrize("method", ["midpoint", "lower"])
def test_quantile_method(self, method) -> None:
q = [0.25, 0.5, 0.75]
actual = DataArray(self.va).quantile(q, method=method)

if Version(np.__version__) >= Version("1.22.0"):
expected = np.nanquantile(self.dv.values, np.array(q), method=method) # type: ignore[call-arg]
else:
expected = np.nanquantile(self.dv.values, np.array(q), interpolation=method) # type: ignore[call-arg]

np.testing.assert_allclose(actual.values, expected)

@pytest.mark.parametrize("method", ["midpoint", "lower"])
def test_quantile_interpolation_deprecated(self, method) -> None:

da = DataArray(self.va)
q = [0.25, 0.5, 0.75]

with pytest.warns(
FutureWarning,
match="`interpolation` argument to quantile was renamed to `method`",
):
actual = da.quantile(q, interpolation=method)

expected = da.quantile(q, method=method)

np.testing.assert_allclose(actual.values, expected.values)

with warnings.catch_warnings(record=True):
with pytest.raises(TypeError, match="interpolation and method keywords"):
da.quantile(q, method=method, interpolation=method)

def test_reduce_keep_attrs(self):
# Test dropped attrs
vm = self.va.mean()
Expand Down
Loading