Skip to content

Commit 9fefefb

Browse files
authored
Speed up Dataset._construct_dataarray (#4744)
* Speed up Dataset._construct_dataarray * Preserve ordering
1 parent 8731acc commit 9fefefb

File tree

2 files changed

+5
-4
lines changed

2 files changed

+5
-4
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ Breaking changes
3333

3434
New Features
3535
~~~~~~~~~~~~
36-
36+
- Performance improvement when constructing DataArrays. Significantly speeds up repr for Datasets with large number of variables.
37+
By `Deepak Cherian <https://github.com/dcherian>`_
3738

3839
Bug fixes
3940
~~~~~~~~~
40-
4141
- :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>`_.
4242
- :py:func:`merge` with ``combine_attrs='override'`` makes a copy of the attrs (:issue:`4627`).
4343
- By default, when possible, xarray will now always use values of type ``int64`` when encoding

xarray/core/dataset.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,8 +1320,9 @@ def _construct_dataarray(self, name: Hashable) -> "DataArray":
13201320
needed_dims = set(variable.dims)
13211321

13221322
coords: Dict[Hashable, Variable] = {}
1323-
for k in self.coords:
1324-
if set(self.variables[k].dims) <= needed_dims:
1323+
# preserve ordering
1324+
for k in self._variables:
1325+
if k in self._coord_names and set(self.variables[k].dims) <= needed_dims:
13251326
coords[k] = self.variables[k]
13261327

13271328
if self._indexes is None:

0 commit comments

Comments
 (0)