Skip to content
forked from pydata/xarray

Commit

Permalink
Merge remote-tracking branch 'upstream/master' into fix/groupby-reduc…
Browse files Browse the repository at this point in the history
…e-multiple-dims

* upstream/master:
  change ALL_DIMS to equal ellipsis (pydata#3418)
  Escaping dtypes (pydata#3444)
  Html repr (pydata#3425)
  • Loading branch information
dcherian committed Oct 25, 2019
2 parents 4d14fca + 79b3cdd commit 228c5c7
Show file tree
Hide file tree
Showing 21 changed files with 856 additions and 46 deletions.
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ prune doc/generated
global-exclude .DS_Store
include versioneer.py
include xarray/_version.py
recursive-include xarray/static *
2 changes: 1 addition & 1 deletion doc/examples/multidimensional-coords.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ function to specify the output coordinates of the group.
lat_center = np.arange(1, 90, 2)
# group according to those bins and take the mean
Tair_lat_mean = (ds.Tair.groupby_bins('xc', lat_bins, labels=lat_center)
.mean(xr.ALL_DIMS))
.mean(...))
# plot the result
@savefig xarray_multidimensional_coords_14_1.png width=5in
Tair_lat_mean.plot();
Expand Down
16 changes: 11 additions & 5 deletions doc/groupby.rst
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,13 @@ dimensions *other than* the provided one:

.. ipython:: python
ds.groupby('x').std(xr.ALL_DIMS)
ds.groupby('x').std(...)
.. note::

We use an ellipsis (`...`) here to indicate we want to reduce over all
other dimensions


First and last
~~~~~~~~~~~~~~
Expand All @@ -127,7 +133,7 @@ values for group along the grouped dimension:

.. ipython:: python
ds.groupby('letters').first(xr.ALL_DIMS)
ds.groupby('letters').first(...)
By default, they skip missing values (control this with ``skipna``).

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

.. ipython:: python
alt = arr.groupby('letters').mean(xr.ALL_DIMS)
alt = arr.groupby('letters').mean(...)
alt
ds.groupby('letters') - alt
Expand Down Expand Up @@ -195,7 +201,7 @@ __ http://cfconventions.org/cf-conventions/v1.6.0/cf-conventions.html#_two_dimen
'lat': (['ny','nx'], [[10,10],[20,20]] ),},
dims=['ny','nx'])
da
da.groupby('lon').sum(xr.ALL_DIMS)
da.groupby('lon').sum(...)
da.groupby('lon').apply(lambda x: x - x.mean(), shortcut=False)
Because multidimensional groups have the ability to generate a very large
Expand All @@ -213,4 +219,4 @@ applying your function, and then unstacking the result:
.. ipython:: python
stacked = da.stack(gridcell=['ny', 'nx'])
stacked.groupby('gridcell').sum(xr.ALL_DIMS).unstack('gridcell')
stacked.groupby('gridcell').sum(...).unstack('gridcell')
11 changes: 11 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ Breaking changes

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

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

- Added new :py:meth:`Dataset._repr_html_` and :py:meth:`DataArray._repr_html_` to improve
representation of objects in jupyter. By default this feature is turned off
for now. Enable it with :py:meth:`xarray.set_options(display_style="html")`.
(:pull:`3425`) by `Benoit Bovy <https://github.com/benbovy>`_ and
`Julia Signell <https://github.com/jsignell>`_.

Bug fixes
~~~~~~~~~
- Fix regression introduced in v0.14.0 that would cause a crash if dask is installed
Expand Down
4 changes: 3 additions & 1 deletion setup.py
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -104,5 +104,7 @@
tests_require=TESTS_REQUIRE,
url=URL,
packages=find_packages(),
package_data={"xarray": ["py.typed", "tests/data/*"]},
package_data={
"xarray": ["py.typed", "tests/data/*", "static/css/*", "static/html/*"]
},
)
14 changes: 10 additions & 4 deletions xarray/core/common.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import warnings
from contextlib import suppress
from html import escape
from textwrap import dedent
from typing import (
Any,
Expand All @@ -18,16 +19,16 @@
import numpy as np
import pandas as pd

from . import dtypes, duck_array_ops, formatting, ops
from . import dtypes, duck_array_ops, formatting, formatting_html, ops
from .arithmetic import SupportsArithmetic
from .npcompat import DTypeLike
from .options import _get_keep_attrs
from .options import OPTIONS, _get_keep_attrs
from .pycompat import dask_array_type
from .rolling_exp import RollingExp
from .utils import Frozen, ReprObject, either_dict_or_kwargs
from .utils import Frozen, either_dict_or_kwargs

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


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

def _repr_html_(self):
if OPTIONS["display_style"] == "text":
return f"<pre>{escape(repr(self))}</pre>"
return formatting_html.array_repr(self)

def _iter(self: Any) -> Iterator[Any]:
for n in range(len(self)):
yield self[n]
Expand Down
12 changes: 9 additions & 3 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import sys
import warnings
from collections import defaultdict
from html import escape
from numbers import Number
from pathlib import Path
from typing import (
Expand Down Expand Up @@ -39,6 +40,7 @@
dtypes,
duck_array_ops,
formatting,
formatting_html,
groupby,
ops,
resample,
Expand All @@ -47,7 +49,6 @@
)
from .alignment import _broadcast_helper, _get_broadcast_dims_map_common_coords, align
from .common import (
ALL_DIMS,
DataWithCoords,
ImplementsDatasetReduce,
_contains_datetime_like_objects,
Expand Down Expand Up @@ -1619,6 +1620,11 @@ def to_zarr(
def __repr__(self) -> str:
return formatting.dataset_repr(self)

def _repr_html_(self):
if OPTIONS["display_style"] == "text":
return f"<pre>{escape(repr(self))}</pre>"
return formatting_html.dataset_repr(self)

def info(self, buf=None) -> None:
"""
Concise summary of a Dataset variables and attributes.
Expand Down Expand Up @@ -4030,7 +4036,7 @@ def reduce(
Dataset with this object's DataArrays replaced with new DataArrays
of summarized data and the indicated dimension(s) removed.
"""
if dim is None or dim is ALL_DIMS:
if dim is None or dim is ...:
dims = set(self.dims)
elif isinstance(dim, str) or not isinstance(dim, Iterable):
dims = {dim}
Expand Down Expand Up @@ -4995,7 +5001,7 @@ def quantile(

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

0 comments on commit 228c5c7

Please sign in to comment.