Skip to content

Commit 99a5adc

Browse files
authored
Deprecation: groupby, resample default dim. (#3313)
* Deprecation: groupby, resample default dim. * fix whats-new * found another test to fix.
1 parent d087fc5 commit 99a5adc

File tree

7 files changed

+17
-140
lines changed

7 files changed

+17
-140
lines changed

doc/whats-new.rst

+3
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ Breaking changes
5454
error in a later release.
5555

5656
(:issue:`3250`) by `Guido Imperiale <https://github.com/crusaderky>`_.
57+
- The default dimension for :py:meth:`~xarray.Dataset.groupby`, :py:meth:`~xarray.Dataset.resample`,
58+
:py:meth:`~xarray.DataArray.groupby` and :py:meth:`~xarray.DataArray.resample` reductions is now the
59+
grouping or resampling dimension.
5760
- :py:meth:`~Dataset.to_dataset` requires ``name`` to be passed as a kwarg (previously ambiguous
5861
positional arguments were deprecated)
5962
- Reindexing with variables of a different dimension now raise an error (previously deprecated)

xarray/core/dataset.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -3875,9 +3875,7 @@ def reduce(
38753875
Dataset with this object's DataArrays replaced with new DataArrays
38763876
of summarized data and the indicated dimension(s) removed.
38773877
"""
3878-
if dim is ALL_DIMS:
3879-
dim = None
3880-
if dim is None:
3878+
if dim is None or dim is ALL_DIMS:
38813879
dims = set(self.dims)
38823880
elif isinstance(dim, str) or not isinstance(dim, Iterable):
38833881
dims = {dim}
@@ -4803,7 +4801,7 @@ def quantile(
48034801

48044802
if isinstance(dim, str):
48054803
dims = {dim}
4806-
elif dim is None:
4804+
elif dim is None or dim is ALL_DIMS:
48074805
dims = set(self.dims)
48084806
else:
48094807
dims = set(dim)

xarray/core/groupby.py

+5-104
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
import numpy as np
66
import pandas as pd
77

8-
from . import dtypes, duck_array_ops, nputils, ops, utils
8+
from . import dtypes, duck_array_ops, nputils, ops
99
from .arithmetic import SupportsArithmetic
10-
from .common import ALL_DIMS, ImplementsArrayReduce, ImplementsDatasetReduce
10+
from .common import ImplementsArrayReduce, ImplementsDatasetReduce
1111
from .concat import concat
1212
from .options import _get_keep_attrs
1313
from .pycompat import integer_types
@@ -700,19 +700,8 @@ def quantile(self, q, dim=None, interpolation="linear", keep_attrs=None):
700700
numpy.nanpercentile, pandas.Series.quantile, Dataset.quantile,
701701
DataArray.quantile
702702
"""
703-
if dim == DEFAULT_DIMS:
704-
dim = ALL_DIMS
705-
# TODO change this to dim = self._group_dim after
706-
# the deprecation process
707-
if self._obj.ndim > 1:
708-
warnings.warn(
709-
"Default reduction dimension will be changed to the "
710-
"grouped dimension in a future version of xarray. To "
711-
"silence this warning, pass dim=xarray.ALL_DIMS "
712-
"explicitly.",
713-
FutureWarning,
714-
stacklevel=2,
715-
)
703+
if dim is None:
704+
dim = self._group_dim
716705

717706
out = self.apply(
718707
self._obj.__class__.quantile,
@@ -758,20 +747,6 @@ def reduce(
758747
Array with summarized data and the indicated dimension(s)
759748
removed.
760749
"""
761-
if dim == DEFAULT_DIMS:
762-
dim = ALL_DIMS
763-
# TODO change this to dim = self._group_dim after
764-
# the deprecation process
765-
if self._obj.ndim > 1:
766-
warnings.warn(
767-
"Default reduction dimension will be changed to the "
768-
"grouped dimension in a future version of xarray. To "
769-
"silence this warning, pass dim=xarray.ALL_DIMS "
770-
"explicitly.",
771-
FutureWarning,
772-
stacklevel=2,
773-
)
774-
775750
if keep_attrs is None:
776751
keep_attrs = _get_keep_attrs(default=False)
777752

@@ -780,43 +755,6 @@ def reduce_array(ar):
780755

781756
return self.apply(reduce_array, shortcut=shortcut)
782757

783-
# TODO remove the following class method and DEFAULT_DIMS after the
784-
# deprecation cycle
785-
@classmethod
786-
def _reduce_method(cls, func, include_skipna, numeric_only):
787-
if include_skipna:
788-
789-
def wrapped_func(
790-
self,
791-
dim=DEFAULT_DIMS,
792-
axis=None,
793-
skipna=None,
794-
keep_attrs=None,
795-
**kwargs
796-
):
797-
return self.reduce(
798-
func,
799-
dim,
800-
axis,
801-
keep_attrs=keep_attrs,
802-
skipna=skipna,
803-
allow_lazy=True,
804-
**kwargs
805-
)
806-
807-
else:
808-
809-
def wrapped_func( # type: ignore
810-
self, dim=DEFAULT_DIMS, axis=None, keep_attrs=None, **kwargs
811-
):
812-
return self.reduce(
813-
func, dim, axis, keep_attrs=keep_attrs, allow_lazy=True, **kwargs
814-
)
815-
816-
return wrapped_func
817-
818-
819-
DEFAULT_DIMS = utils.ReprObject("<default-dims>")
820758

821759
ops.inject_reduce_methods(DataArrayGroupBy)
822760
ops.inject_binary_ops(DataArrayGroupBy)
@@ -898,19 +836,7 @@ def reduce(self, func, dim=None, keep_attrs=None, **kwargs):
898836
Array with summarized data and the indicated dimension(s)
899837
removed.
900838
"""
901-
if dim == DEFAULT_DIMS:
902-
dim = ALL_DIMS
903-
# TODO change this to dim = self._group_dim after
904-
# the deprecation process. Do not forget to remove _reduce_method
905-
warnings.warn(
906-
"Default reduction dimension will be changed to the "
907-
"grouped dimension in a future version of xarray. To "
908-
"silence this warning, pass dim=xarray.ALL_DIMS "
909-
"explicitly.",
910-
FutureWarning,
911-
stacklevel=2,
912-
)
913-
elif dim is None:
839+
if dim is None:
914840
dim = self._group_dim
915841

916842
if keep_attrs is None:
@@ -921,31 +847,6 @@ def reduce_dataset(ds):
921847

922848
return self.apply(reduce_dataset)
923849

924-
# TODO remove the following class method and DEFAULT_DIMS after the
925-
# deprecation cycle
926-
@classmethod
927-
def _reduce_method(cls, func, include_skipna, numeric_only):
928-
if include_skipna:
929-
930-
def wrapped_func(self, dim=DEFAULT_DIMS, skipna=None, **kwargs):
931-
return self.reduce(
932-
func,
933-
dim,
934-
skipna=skipna,
935-
numeric_only=numeric_only,
936-
allow_lazy=True,
937-
**kwargs
938-
)
939-
940-
else:
941-
942-
def wrapped_func(self, dim=DEFAULT_DIMS, **kwargs): # type: ignore
943-
return self.reduce(
944-
func, dim, numeric_only=numeric_only, allow_lazy=True, **kwargs
945-
)
946-
947-
return wrapped_func
948-
949850
def assign(self, **kwargs):
950851
"""Assign data variables by group.
951852

xarray/core/resample.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from . import ops
2-
from .groupby import DEFAULT_DIMS, DataArrayGroupBy, DatasetGroupBy
2+
from .groupby import DataArrayGroupBy, DatasetGroupBy
33

44
RESAMPLE_DIM = "__resample_dim__"
55

@@ -307,9 +307,6 @@ def reduce(self, func, dim=None, keep_attrs=None, **kwargs):
307307
Array with summarized data and the indicated dimension(s)
308308
removed.
309309
"""
310-
if dim == DEFAULT_DIMS:
311-
dim = None
312-
313310
return super().reduce(func, dim, keep_attrs, **kwargs)
314311

315312

xarray/tests/test_dataarray.py

+1-11
Original file line numberDiff line numberDiff line change
@@ -2499,16 +2499,6 @@ def test_groupby_sum(self):
24992499
assert_allclose(expected_sum_axis1, grouped.reduce(np.sum, "y"))
25002500
assert_allclose(expected_sum_axis1, grouped.sum("y"))
25012501

2502-
def test_groupby_warning(self):
2503-
array = self.make_groupby_example_array()
2504-
grouped = array.groupby("y")
2505-
with pytest.warns(FutureWarning):
2506-
grouped.sum()
2507-
2508-
@pytest.mark.skipif(
2509-
LooseVersion(xr.__version__) < LooseVersion("0.13"),
2510-
reason="not to forget the behavior change",
2511-
)
25122502
def test_groupby_sum_default(self):
25132503
array = self.make_groupby_example_array()
25142504
grouped = array.groupby("abc")
@@ -2529,7 +2519,7 @@ def test_groupby_sum_default(self):
25292519
}
25302520
)["foo"]
25312521

2532-
assert_allclose(expected_sum_all, grouped.sum())
2522+
assert_allclose(expected_sum_all, grouped.sum(dim="y"))
25332523

25342524
def test_groupby_count(self):
25352525
array = DataArray(

xarray/tests/test_dataset.py

-12
Original file line numberDiff line numberDiff line change
@@ -3367,18 +3367,6 @@ def test_groupby_reduce(self):
33673367
actual = data.groupby("letters").mean(ALL_DIMS)
33683368
assert_allclose(expected, actual)
33693369

3370-
def test_groupby_warn(self):
3371-
data = Dataset(
3372-
{
3373-
"xy": (["x", "y"], np.random.randn(3, 4)),
3374-
"xonly": ("x", np.random.randn(3)),
3375-
"yonly": ("y", np.random.randn(4)),
3376-
"letters": ("y", ["a", "a", "b", "b"]),
3377-
}
3378-
)
3379-
with pytest.warns(FutureWarning):
3380-
data.groupby("x").mean()
3381-
33823370
def test_groupby_math(self):
33833371
def reorder_dims(x):
33843372
return x.transpose("dim1", "dim2", "dim3", "time")

xarray/tests/test_groupby.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -134,21 +134,21 @@ def test_da_groupby_quantile():
134134
[("x", [1, 1, 1, 2, 2]), ("y", [0, 0, 1])],
135135
)
136136

137-
actual_x = array.groupby("x").quantile(0)
137+
actual_x = array.groupby("x").quantile(0, dim=xr.ALL_DIMS)
138138
expected_x = xr.DataArray([1, 4], [("x", [1, 2])])
139139
assert_identical(expected_x, actual_x)
140140

141-
actual_y = array.groupby("y").quantile(0)
141+
actual_y = array.groupby("y").quantile(0, dim=xr.ALL_DIMS)
142142
expected_y = xr.DataArray([1, 22], [("y", [0, 1])])
143143
assert_identical(expected_y, actual_y)
144144

145-
actual_xx = array.groupby("x").quantile(0, dim="x")
145+
actual_xx = array.groupby("x").quantile(0)
146146
expected_xx = xr.DataArray(
147147
[[1, 11, 22], [4, 15, 24]], [("x", [1, 2]), ("y", [0, 0, 1])]
148148
)
149149
assert_identical(expected_xx, actual_xx)
150150

151-
actual_yy = array.groupby("y").quantile(0, dim="y")
151+
actual_yy = array.groupby("y").quantile(0)
152152
expected_yy = xr.DataArray(
153153
[[1, 26], [2, 22], [3, 23], [4, 24], [5, 25]],
154154
[("x", [1, 1, 1, 2, 2]), ("y", [0, 1])],
@@ -164,7 +164,7 @@ def test_da_groupby_quantile():
164164
)
165165
g = foo.groupby(foo.time.dt.month)
166166

167-
actual = g.quantile(0)
167+
actual = g.quantile(0, dim=xr.ALL_DIMS)
168168
expected = xr.DataArray(
169169
[
170170
0.0,

0 commit comments

Comments
 (0)