Skip to content

Commit

Permalink
Speed up Dataset._construct_dataarray (#4744)
Browse files Browse the repository at this point in the history
* Speed up Dataset._construct_dataarray

* Preserve ordering
  • Loading branch information
dcherian authored Jan 5, 2021
1 parent 8731acc commit 9fefefb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
4 changes: 2 additions & 2 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ Breaking changes

New Features
~~~~~~~~~~~~

- Performance improvement when constructing DataArrays. Significantly speeds up repr for Datasets with large number of variables.
By `Deepak Cherian <https://github.com/dcherian>`_

Bug fixes
~~~~~~~~~

- :py:meth:`DataArray.resample` and :py:meth:`Dataset.resample` do not trigger computations anymore if :py:meth:`Dataset.weighted` or :py:meth:`DataArray.weighted` are applied (:issue:`4625`, :pull:`4668`). By `Julius Busecke <https://github.com/jbusecke>`_.
- :py:func:`merge` with ``combine_attrs='override'`` makes a copy of the attrs (:issue:`4627`).
- By default, when possible, xarray will now always use values of type ``int64`` when encoding
Expand Down
5 changes: 3 additions & 2 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1320,8 +1320,9 @@ def _construct_dataarray(self, name: Hashable) -> "DataArray":
needed_dims = set(variable.dims)

coords: Dict[Hashable, Variable] = {}
for k in self.coords:
if set(self.variables[k].dims) <= needed_dims:
# preserve ordering
for k in self._variables:
if k in self._coord_names and set(self.variables[k].dims) <= needed_dims:
coords[k] = self.variables[k]

if self._indexes is None:
Expand Down

0 comments on commit 9fefefb

Please sign in to comment.