Skip to content

Commit bde59f2

Browse files
committed
changed references & added whatsnew
1 parent 514219e commit bde59f2

File tree

12 files changed

+47
-44
lines changed

12 files changed

+47
-44
lines changed

doc/examples/multidimensional-coords.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ function to specify the output coordinates of the group.
107107
lat_center = np.arange(1, 90, 2)
108108
# group according to those bins and take the mean
109109
Tair_lat_mean = (ds.Tair.groupby_bins('xc', lat_bins, labels=lat_center)
110-
.mean(xr.ALL_DIMS))
110+
.mean(...))
111111
# plot the result
112112
@savefig xarray_multidimensional_coords_14_1.png width=5in
113113
Tair_lat_mean.plot();

doc/groupby.rst

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ dimensions *other than* the provided one:
116116

117117
.. ipython:: python
118118
119-
ds.groupby('x').std(xr.ALL_DIMS)
119+
ds.groupby('x').std(...)
120120
121121
First and last
122122
~~~~~~~~~~~~~~
@@ -127,7 +127,7 @@ values for group along the grouped dimension:
127127

128128
.. ipython:: python
129129
130-
ds.groupby('letters').first(xr.ALL_DIMS)
130+
ds.groupby('letters').first(...)
131131
132132
By default, they skip missing values (control this with ``skipna``).
133133

@@ -142,7 +142,7 @@ coordinates. For example:
142142

143143
.. ipython:: python
144144
145-
alt = arr.groupby('letters').mean(xr.ALL_DIMS)
145+
alt = arr.groupby('letters').mean(...)
146146
alt
147147
ds.groupby('letters') - alt
148148
@@ -195,7 +195,7 @@ __ http://cfconventions.org/cf-conventions/v1.6.0/cf-conventions.html#_two_dimen
195195
'lat': (['ny','nx'], [[10,10],[20,20]] ),},
196196
dims=['ny','nx'])
197197
da
198-
da.groupby('lon').sum(xr.ALL_DIMS)
198+
da.groupby('lon').sum(...)
199199
da.groupby('lon').apply(lambda x: x - x.mean(), shortcut=False)
200200
201201
Because multidimensional groups have the ability to generate a very large
@@ -213,4 +213,4 @@ applying your function, and then unstacking the result:
213213
.. ipython:: python
214214
215215
stacked = da.stack(gridcell=['ny', 'nx'])
216-
stacked.groupby('gridcell').sum(xr.ALL_DIMS).unstack('gridcell')
216+
stacked.groupby('gridcell').sum(...).unstack('gridcell')

doc/whats-new.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ v0.14.1 (unreleased)
2020

2121
New Features
2222
~~~~~~~~~~~~
23+
- Changed `xr.ALL_DIMS` to equal python's `Ellipsis` (`...`), and changed internal usages to use
24+
`...` directly. As before, you can use this to instruct a `groupby` operation
25+
to reduce over all dimensions. While we have no plans to remove `xr.ALL_DIMS`, we suggest
26+
using `...`.
27+
By `Maximilian Roos <https://github.com/max-sixty>`_
2328
- Added integration tests against `pint <https://pint.readthedocs.io/>`_.
2429
(:pull:`3238`) by `Justus Magin <https://github.com/keewis>`_.
2530

xarray/core/dataset.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@
4747
)
4848
from .alignment import _broadcast_helper, _get_broadcast_dims_map_common_coords, align
4949
from .common import (
50-
ALL_DIMS,
5150
DataWithCoords,
5251
ImplementsDatasetReduce,
5352
_contains_datetime_like_objects,
@@ -4032,7 +4031,7 @@ def reduce(
40324031
Dataset with this object's DataArrays replaced with new DataArrays
40334032
of summarized data and the indicated dimension(s) removed.
40344033
"""
4035-
if dim is None or dim is ALL_DIMS:
4034+
if dim is None or dim is ...:
40364035
dims = set(self.dims)
40374036
elif isinstance(dim, str) or not isinstance(dim, Iterable):
40384037
dims = {dim}
@@ -4997,7 +4996,7 @@ def quantile(
49974996

49984997
if isinstance(dim, str):
49994998
dims = {dim}
5000-
elif dim is None or dim is ALL_DIMS:
4999+
elif dim in [None, ...]:
50015000
dims = set(self.dims)
50025001
else:
50035002
dims = set(dim)

xarray/core/groupby.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
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 .formatting import format_array_flat
1313
from .options import _get_keep_attrs
@@ -712,7 +712,7 @@ def quantile(self, q, dim=None, interpolation="linear", keep_attrs=None):
712712
q : float in range of [0,1] (or sequence of floats)
713713
Quantile to compute, which must be between 0 and 1
714714
inclusive.
715-
dim : xarray.ALL_DIMS (or `...`), str or sequence of str, optional
715+
dim : `...`, str or sequence of str, optional
716716
Dimension(s) over which to apply quantile.
717717
Defaults to the grouped dimension.
718718
interpolation : {'linear', 'lower', 'higher', 'midpoint', 'nearest'}
@@ -769,7 +769,7 @@ def reduce(
769769
Function which can be called in the form
770770
`func(x, axis=axis, **kwargs)` to return the result of collapsing
771771
an np.ndarray over an integer valued axis.
772-
dim : xarray.ALL_DIMS (or `...`), str or sequence of str, optional
772+
dim : `...`, str or sequence of str, optional
773773
Dimension(s) over which to apply `func`.
774774
axis : int or sequence of int, optional
775775
Axis(es) over which to apply `func`. Only one of the 'dimension'
@@ -794,9 +794,9 @@ def reduce(
794794
if keep_attrs is None:
795795
keep_attrs = _get_keep_attrs(default=False)
796796

797-
if dim is not ALL_DIMS and dim not in self.dims:
797+
if dim is not ... and dim not in self.dims:
798798
raise ValueError(
799-
"cannot reduce over dimension %r. expected either xarray.ALL_DIMS (or `...`) to reduce over all dimensions or one or more of %r."
799+
"cannot reduce over dimension %r. expected either `...` to reduce over all dimensions or one or more of %r."
800800
% (dim, self.dims)
801801
)
802802

@@ -867,7 +867,7 @@ def reduce(self, func, dim=None, keep_attrs=None, **kwargs):
867867
Function which can be called in the form
868868
`func(x, axis=axis, **kwargs)` to return the result of collapsing
869869
an np.ndarray over an integer valued axis.
870-
dim : xarray.ALL_DIMS (or `...`), str or sequence of str, optional
870+
dim : `...`, str or sequence of str, optional
871871
Dimension(s) over which to apply `func`.
872872
axis : int or sequence of int, optional
873873
Axis(es) over which to apply `func`. Only one of the 'dimension'
@@ -895,9 +895,9 @@ def reduce(self, func, dim=None, keep_attrs=None, **kwargs):
895895
def reduce_dataset(ds):
896896
return ds.reduce(func, dim, keep_attrs, **kwargs)
897897

898-
if dim is not ALL_DIMS and dim not in self.dims:
898+
if dim is not ... and dim not in self.dims:
899899
raise ValueError(
900-
"cannot reduce over dimension %r. expected either xarray.ALL_DIMS (or `...`) to reduce over all dimensions or one or more of %r."
900+
"cannot reduce over dimension %r. expected either `...` to reduce over all dimensions or one or more of %r."
901901
% (dim, self.dims)
902902
)
903903

xarray/core/variable.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ def reduce(
14501450
Array with summarized data and the indicated dimension(s)
14511451
removed.
14521452
"""
1453-
if dim is common.ALL_DIMS:
1453+
if dim == ...:
14541454
dim = None
14551455
if dim is not None and axis is not None:
14561456
raise ValueError("cannot supply both 'axis' and 'dim' arguments")

xarray/tests/test_dask.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -435,8 +435,8 @@ def test_groupby(self):
435435
u = self.eager_array
436436
v = self.lazy_array
437437

438-
expected = u.groupby("x").mean(xr.ALL_DIMS)
439-
actual = v.groupby("x").mean(xr.ALL_DIMS)
438+
expected = u.groupby("x").mean(...)
439+
actual = v.groupby("x").mean(...)
440440
self.assertLazyAndAllClose(expected, actual)
441441

442442
def test_groupby_first(self):

xarray/tests/test_dataarray.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from xarray.coding.times import CFDatetimeCoder
1414
from xarray.convert import from_cdms2
1515
from xarray.core import dtypes
16-
from xarray.core.common import ALL_DIMS, full_like
16+
from xarray.core.common import full_like
1717
from xarray.tests import (
1818
LooseVersion,
1919
ReturnItem,
@@ -2443,8 +2443,8 @@ def test_groupby_sum(self):
24432443
"abc": Variable(["abc"], np.array(["a", "b", "c"])),
24442444
}
24452445
)["foo"]
2446-
assert_allclose(expected_sum_all, grouped.reduce(np.sum, dim=ALL_DIMS))
2447-
assert_allclose(expected_sum_all, grouped.sum(ALL_DIMS))
2446+
assert_allclose(expected_sum_all, grouped.reduce(np.sum, dim=...))
2447+
assert_allclose(expected_sum_all, grouped.sum(...))
24482448

24492449
expected = DataArray(
24502450
[
@@ -2456,7 +2456,7 @@ def test_groupby_sum(self):
24562456
)
24572457
actual = array["y"].groupby("abc").apply(np.sum)
24582458
assert_allclose(expected, actual)
2459-
actual = array["y"].groupby("abc").sum(ALL_DIMS)
2459+
actual = array["y"].groupby("abc").sum(...)
24602460
assert_allclose(expected, actual)
24612461

24622462
expected_sum_axis1 = Dataset(
@@ -2590,9 +2590,9 @@ def test_groupby_math(self):
25902590
assert_identical(expected, actual)
25912591

25922592
grouped = array.groupby("abc")
2593-
expected_agg = (grouped.mean(ALL_DIMS) - np.arange(3)).rename(None)
2593+
expected_agg = (grouped.mean(...) - np.arange(3)).rename(None)
25942594
actual = grouped - DataArray(range(3), [("abc", ["a", "b", "c"])])
2595-
actual_agg = actual.groupby("abc").mean(ALL_DIMS)
2595+
actual_agg = actual.groupby("abc").mean(...)
25962596
assert_allclose(expected_agg, actual_agg)
25972597

25982598
with raises_regex(TypeError, "only support binary ops"):
@@ -2698,7 +2698,7 @@ def test_groupby_multidim(self):
26982698
("lon", DataArray([5, 28, 23], coords=[("lon", [30.0, 40.0, 50.0])])),
26992699
("lat", DataArray([16, 40], coords=[("lat", [10.0, 20.0])])),
27002700
]:
2701-
actual_sum = array.groupby(dim).sum(ALL_DIMS)
2701+
actual_sum = array.groupby(dim).sum(...)
27022702
assert_identical(expected_sum, actual_sum)
27032703

27042704
def test_groupby_multidim_apply(self):

xarray/tests/test_dataset.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
import xarray as xr
1313
from xarray import (
14-
ALL_DIMS,
1514
DataArray,
1615
Dataset,
1716
IndexVariable,
@@ -3327,7 +3326,7 @@ def test_groupby_reduce(self):
33273326

33283327
expected = data.mean("y")
33293328
expected["yonly"] = expected["yonly"].variable.set_dims({"x": 3})
3330-
actual = data.groupby("x").mean(ALL_DIMS)
3329+
actual = data.groupby("x").mean(...)
33313330
assert_allclose(expected, actual)
33323331

33333332
actual = data.groupby("x").mean("y")
@@ -3336,12 +3335,12 @@ def test_groupby_reduce(self):
33363335
letters = data["letters"]
33373336
expected = Dataset(
33383337
{
3339-
"xy": data["xy"].groupby(letters).mean(ALL_DIMS),
3338+
"xy": data["xy"].groupby(letters).mean(...),
33403339
"xonly": (data["xonly"].mean().variable.set_dims({"letters": 2})),
33413340
"yonly": data["yonly"].groupby(letters).mean(),
33423341
}
33433342
)
3344-
actual = data.groupby("letters").mean(ALL_DIMS)
3343+
actual = data.groupby("letters").mean(...)
33453344
assert_allclose(expected, actual)
33463345

33473346
def test_groupby_math(self):
@@ -3404,14 +3403,14 @@ def test_groupby_math_virtual(self):
34043403
{"x": ("t", [1, 2, 3])}, {"t": pd.date_range("20100101", periods=3)}
34053404
)
34063405
grouped = ds.groupby("t.day")
3407-
actual = grouped - grouped.mean(ALL_DIMS)
3406+
actual = grouped - grouped.mean(...)
34083407
expected = Dataset({"x": ("t", [0, 0, 0])}, ds[["t", "t.day"]])
34093408
assert_identical(actual, expected)
34103409

34113410
def test_groupby_nan(self):
34123411
# nan should be excluded from groupby
34133412
ds = Dataset({"foo": ("x", [1, 2, 3, 4])}, {"bar": ("x", [1, 1, 2, np.nan])})
3414-
actual = ds.groupby("bar").mean(ALL_DIMS)
3413+
actual = ds.groupby("bar").mean(...)
34153414
expected = Dataset({"foo": ("bar", [1.5, 3]), "bar": [1, 2]})
34163415
assert_identical(actual, expected)
34173416

@@ -3421,7 +3420,7 @@ def test_groupby_order(self):
34213420
for vn in ["a", "b", "c"]:
34223421
ds[vn] = DataArray(np.arange(10), dims=["t"])
34233422
data_vars_ref = list(ds.data_vars.keys())
3424-
ds = ds.groupby("t").mean(ALL_DIMS)
3423+
ds = ds.groupby("t").mean(...)
34253424
data_vars = list(ds.data_vars.keys())
34263425
assert data_vars == data_vars_ref
34273426
# coords are now at the end of the list, so the test below fails

xarray/tests/test_groupby.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -147,11 +147,11 @@ def test_da_groupby_quantile():
147147
[("x", [1, 1, 1, 2, 2]), ("y", [0, 0, 1])],
148148
)
149149

150-
actual_x = array.groupby("x").quantile(0, dim=xr.ALL_DIMS)
150+
actual_x = array.groupby("x").quantile(0, dim=...)
151151
expected_x = xr.DataArray([1, 4], [("x", [1, 2])])
152152
assert_identical(expected_x, actual_x)
153153

154-
actual_y = array.groupby("y").quantile(0, dim=xr.ALL_DIMS)
154+
actual_y = array.groupby("y").quantile(0, dim=...)
155155
expected_y = xr.DataArray([1, 22], [("y", [0, 1])])
156156
assert_identical(expected_y, actual_y)
157157

@@ -177,7 +177,7 @@ def test_da_groupby_quantile():
177177
)
178178
g = foo.groupby(foo.time.dt.month)
179179

180-
actual = g.quantile(0, dim=xr.ALL_DIMS)
180+
actual = g.quantile(0, dim=...)
181181
expected = xr.DataArray(
182182
[
183183
0.0,

0 commit comments

Comments
 (0)