diff --git a/doc/whats-new.rst b/doc/whats-new.rst index ecc134fc026..d3344ccb941 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -27,11 +27,11 @@ Breaking changes New Features ~~~~~~~~~~~~ - +- Performance improvement when constructing DataArrays. Significantly speeds up repr for Datasets with large number of variables. + By `Deepak Cherian `_ 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 `_. - :py:func:`merge` with ``combine_attrs='override'`` makes a copy of the attrs (:issue:`4627`). - :py:meth:`DataArray.astype`, :py:meth:`Dataset.astype` and :py:meth:`Variable.astype` support diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 3bb5cd8b586..c38e9d63c68 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -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: