Skip to content

Commit 228c5c7

Browse files
committed
Merge remote-tracking branch 'upstream/master' into fix/groupby-reduce-multiple-dims
* upstream/master: change ALL_DIMS to equal ellipsis (pydata#3418) Escaping dtypes (pydata#3444) Html repr (pydata#3425)
2 parents 4d14fca + 79b3cdd commit 228c5c7

21 files changed

+856
-46
lines changed

MANIFEST.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ prune doc/generated
66
global-exclude .DS_Store
77
include versioneer.py
88
include xarray/_version.py
9+
recursive-include xarray/static *

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: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,13 @@ dimensions *other than* the provided one:
116116

117117
.. ipython:: python
118118
119-
ds.groupby('x').std(xr.ALL_DIMS)
119+
ds.groupby('x').std(...)
120+
121+
.. note::
122+
123+
We use an ellipsis (`...`) here to indicate we want to reduce over all
124+
other dimensions
125+
120126

121127
First and last
122128
~~~~~~~~~~~~~~
@@ -127,7 +133,7 @@ values for group along the grouped dimension:
127133

128134
.. ipython:: python
129135
130-
ds.groupby('letters').first(xr.ALL_DIMS)
136+
ds.groupby('letters').first(...)
131137
132138
By default, they skip missing values (control this with ``skipna``).
133139

@@ -142,7 +148,7 @@ coordinates. For example:
142148

143149
.. ipython:: python
144150
145-
alt = arr.groupby('letters').mean(xr.ALL_DIMS)
151+
alt = arr.groupby('letters').mean(...)
146152
alt
147153
ds.groupby('letters') - alt
148154
@@ -195,7 +201,7 @@ __ http://cfconventions.org/cf-conventions/v1.6.0/cf-conventions.html#_two_dimen
195201
'lat': (['ny','nx'], [[10,10],[20,20]] ),},
196202
dims=['ny','nx'])
197203
da
198-
da.groupby('lon').sum(xr.ALL_DIMS)
204+
da.groupby('lon').sum(...)
199205
da.groupby('lon').apply(lambda x: x - x.mean(), shortcut=False)
200206
201207
Because multidimensional groups have the ability to generate a very large
@@ -213,4 +219,4 @@ applying your function, and then unstacking the result:
213219
.. ipython:: python
214220
215221
stacked = da.stack(gridcell=['ny', 'nx'])
216-
stacked.groupby('gridcell').sum(xr.ALL_DIMS).unstack('gridcell')
222+
stacked.groupby('gridcell').sum(...).unstack('gridcell')

doc/whats-new.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ Breaking changes
2525

2626
New Features
2727
~~~~~~~~~~~~
28+
- Changed `xr.ALL_DIMS` to equal python's `Ellipsis` (`...`), and changed internal usages to use
29+
`...` directly. As before, you can use this to instruct a `groupby` operation
30+
to reduce over all dimensions. While we have no plans to remove `xr.ALL_DIMS`, we suggest
31+
using `...`.
32+
By `Maximilian Roos <https://github.com/max-sixty>`_
2833
- Added integration tests against `pint <https://pint.readthedocs.io/>`_.
2934
(:pull:`3238`) by `Justus Magin <https://github.com/keewis>`_.
3035

@@ -36,6 +41,12 @@ New Features
3641
``pip install git+https://github.com/andrewgsavage/pint.git@refs/pull/6/head)``.
3742
Even with it, interaction with non-numpy array libraries, e.g. dask or sparse, is broken.
3843

44+
- Added new :py:meth:`Dataset._repr_html_` and :py:meth:`DataArray._repr_html_` to improve
45+
representation of objects in jupyter. By default this feature is turned off
46+
for now. Enable it with :py:meth:`xarray.set_options(display_style="html")`.
47+
(:pull:`3425`) by `Benoit Bovy <https://github.com/benbovy>`_ and
48+
`Julia Signell <https://github.com/jsignell>`_.
49+
3950
Bug fixes
4051
~~~~~~~~~
4152
- Fix regression introduced in v0.14.0 that would cause a crash if dask is installed

setup.py

100644100755
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,5 +104,7 @@
104104
tests_require=TESTS_REQUIRE,
105105
url=URL,
106106
packages=find_packages(),
107-
package_data={"xarray": ["py.typed", "tests/data/*"]},
107+
package_data={
108+
"xarray": ["py.typed", "tests/data/*", "static/css/*", "static/html/*"]
109+
},
108110
)

xarray/core/common.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import warnings
22
from contextlib import suppress
3+
from html import escape
34
from textwrap import dedent
45
from typing import (
56
Any,
@@ -18,16 +19,16 @@
1819
import numpy as np
1920
import pandas as pd
2021

21-
from . import dtypes, duck_array_ops, formatting, ops
22+
from . import dtypes, duck_array_ops, formatting, formatting_html, ops
2223
from .arithmetic import SupportsArithmetic
2324
from .npcompat import DTypeLike
24-
from .options import _get_keep_attrs
25+
from .options import OPTIONS, _get_keep_attrs
2526
from .pycompat import dask_array_type
2627
from .rolling_exp import RollingExp
27-
from .utils import Frozen, ReprObject, either_dict_or_kwargs
28+
from .utils import Frozen, either_dict_or_kwargs
2829

2930
# Used as a sentinel value to indicate a all dimensions
30-
ALL_DIMS = ReprObject("<all-dims>")
31+
ALL_DIMS = ...
3132

3233

3334
C = TypeVar("C")
@@ -134,6 +135,11 @@ def __array__(self: Any, dtype: DTypeLike = None) -> np.ndarray:
134135
def __repr__(self) -> str:
135136
return formatting.array_repr(self)
136137

138+
def _repr_html_(self):
139+
if OPTIONS["display_style"] == "text":
140+
return f"<pre>{escape(repr(self))}</pre>"
141+
return formatting_html.array_repr(self)
142+
137143
def _iter(self: Any) -> Iterator[Any]:
138144
for n in range(len(self)):
139145
yield self[n]

xarray/core/dataset.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import sys
44
import warnings
55
from collections import defaultdict
6+
from html import escape
67
from numbers import Number
78
from pathlib import Path
89
from typing import (
@@ -39,6 +40,7 @@
3940
dtypes,
4041
duck_array_ops,
4142
formatting,
43+
formatting_html,
4244
groupby,
4345
ops,
4446
resample,
@@ -47,7 +49,6 @@
4749
)
4850
from .alignment import _broadcast_helper, _get_broadcast_dims_map_common_coords, align
4951
from .common import (
50-
ALL_DIMS,
5152
DataWithCoords,
5253
ImplementsDatasetReduce,
5354
_contains_datetime_like_objects,
@@ -1619,6 +1620,11 @@ def to_zarr(
16191620
def __repr__(self) -> str:
16201621
return formatting.dataset_repr(self)
16211622

1623+
def _repr_html_(self):
1624+
if OPTIONS["display_style"] == "text":
1625+
return f"<pre>{escape(repr(self))}</pre>"
1626+
return formatting_html.dataset_repr(self)
1627+
16221628
def info(self, buf=None) -> None:
16231629
"""
16241630
Concise summary of a Dataset variables and attributes.
@@ -4030,7 +4036,7 @@ def reduce(
40304036
Dataset with this object's DataArrays replaced with new DataArrays
40314037
of summarized data and the indicated dimension(s) removed.
40324038
"""
4033-
if dim is None or dim is ALL_DIMS:
4039+
if dim is None or dim is ...:
40344040
dims = set(self.dims)
40354041
elif isinstance(dim, str) or not isinstance(dim, Iterable):
40364042
dims = {dim}
@@ -4995,7 +5001,7 @@ def quantile(
49955001

49965002
if isinstance(dim, str):
49975003
dims = {dim}
4998-
elif dim is None or dim is ALL_DIMS:
5004+
elif dim in [None, ...]:
49995005
dims = set(self.dims)
50005006
else:
50015007
dims = set(dim)

0 commit comments

Comments
 (0)