Skip to content

Commit b2c1550

Browse files
authored
Keep the original ordering of the coordinates (#4409)
* un-xfail the pint assert_allclose and assert_duckarray_equal tests * update the required version of pint * keep the order of the coordinates * fix the groupby doctest * keep the order of the dims in concat * don't compute a set difference if we're filtering anyways * sort names instead of potentially dropping items * Apply suggestions from code review * sort in DatasetCoordinates.to_dataset instead of in Dataset._copy_listed * update whats-new.rst * filter _variables instead of sorting _coord_name
1 parent fd3eb21 commit b2c1550

File tree

6 files changed

+26
-22
lines changed

6 files changed

+26
-22
lines changed

ci/requirements/py36-min-nep18.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ dependencies:
1010
- distributed=2.9
1111
- numpy=1.17
1212
- pandas=0.25
13-
- pint=0.13
13+
- pint=0.15
1414
- pip
1515
- pytest
1616
- pytest-cov

doc/whats-new.rst

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ New Features
3333
now accept more than 1 dimension. (:pull:`4219`)
3434
By `Keisuke Fujii <https://github.com/fujiisoup>`_.
3535
- ``min_count`` can be supplied to reductions such as ``.sum`` when specifying
36-
multiple dimension to reduce over. (:pull:`4356`)
36+
multiple dimension to reduce over. (:pull:`4356`)
3737
By `Maximilian Roos <https://github.com/max-sixty>`_.
3838
- :py:func:`xarray.cov` and :py:func:`xarray.corr` now handle missing values. (:pull:`4351`)
3939
By `Maximilian Roos <https://github.com/max-sixty>`_.
@@ -77,7 +77,7 @@ Bug fixes
7777
and :py:meth:`DataArray.str.wrap` (:issue:`4334`). By `Mathias Hauser <https://github.com/mathause>`_.
7878
- Fixed overflow issue causing incorrect results in computing means of :py:class:`cftime.datetime`
7979
arrays (:issue:`4341`). By `Spencer Clark <https://github.com/spencerkclark>`_.
80-
- Fixed :py:meth:`Dataset.coarsen`, :py:meth:`DataArray.coarsen` dropping attributes on original object (:issue:`4120`, :pull:`4360`). by `Julia Kent <https://github.com/jukent>`_.
80+
- Fixed :py:meth:`Dataset.coarsen`, :py:meth:`DataArray.coarsen` dropping attributes on original object (:issue:`4120`, :pull:`4360`). By `Julia Kent <https://github.com/jukent>`_.
8181
- fix the signature of the plot methods. (:pull:`4359`) By `Justus Magin <https://github.com/keewis>`_.
8282
- Fix :py:func:`xarray.apply_ufunc` with ``vectorize=True`` and ``exclude_dims`` (:issue:`3890`).
8383
By `Mathias Hauser <https://github.com/mathause>`_.
@@ -86,6 +86,8 @@ Bug fixes
8686
By `Jens Svensmark <https://github.com/jenssss>`_
8787
- Fix incorrect legend labels for :py:meth:`Dataset.plot.scatter` (:issue:`4126`).
8888
By `Peter Hausamann <https://github.com/phausamann>`_.
89+
- Avoid relying on :py:class:`set` objects for the ordering of the coordinates (:pull:`4409`)
90+
By `Justus Magin <https://github.com/keewis>`_.
8991
- Fix indexing with datetime64 scalars with pandas 1.1 (:issue:`4283`).
9092
By `Stephan Hoyer <https://github.com/shoyer>`_ and
9193
`Justus Magin <https://github.com/keewis>`_.
@@ -99,6 +101,8 @@ Documentation
99101
By `Sander van Rijn <https://github.com/sjvrijn>`_
100102
- Update the contributing guide to use merges instead of rebasing and state
101103
that we squash-merge. (:pull:`4355`) By `Justus Magin <https://github.com/keewis>`_.
104+
- Make sure the examples from the docstrings actually work (:pull:`4408`).
105+
By `Justus Magin <https://github.com/keewis>`_.
102106
- Updated Vectorized Indexing to a clearer example.
103107
By `Maximilian Roos <https://github.com/max-sixty>`_
104108

xarray/core/concat.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,11 @@ def _parse_datasets(
349349
all_coord_names.update(ds.coords)
350350
data_vars.update(ds.data_vars)
351351

352-
for dim in set(ds.dims) - dims:
352+
# preserves ordering of dimensions
353+
for dim in ds.dims:
354+
if dim in dims:
355+
continue
356+
353357
if dim not in dim_coords:
354358
dim_coords[dim] = ds.coords[dim].variable
355359
dims = dims | set(ds.dims)

xarray/core/coordinates.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,9 @@ def __getitem__(self, key: Hashable) -> "DataArray":
215215

216216
def to_dataset(self) -> "Dataset":
217217
"""Convert these coordinates into a new Dataset"""
218-
return self._data._copy_listed(self._names)
218+
219+
names = [name for name in self._data._variables if name in self._names]
220+
return self._data._copy_listed(names)
219221

220222
def _update_coords(
221223
self, coords: Dict[Hashable, Variable], indexes: Mapping[Hashable, pd.Index]

xarray/core/dataset.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,11 @@ def _copy_listed(self, names: Iterable[Hashable]) -> "Dataset":
11421142

11431143
dims = {k: self.dims[k] for k in needed_dims}
11441144

1145-
for k in self._coord_names:
1145+
# preserves ordering of coordinates
1146+
for k in self._variables:
1147+
if k not in self._coord_names:
1148+
continue
1149+
11461150
if set(self.variables[k].dims) <= needed_dims:
11471151
variables[k] = self._variables[k]
11481152
coord_names.add(k)
@@ -5729,10 +5733,10 @@ def filter_by_attrs(self, **kwargs):
57295733
<xarray.Dataset>
57305734
Dimensions: (time: 3, x: 2, y: 2)
57315735
Coordinates:
5732-
reference_time datetime64[ns] 2014-09-05
5736+
lon (x, y) float64 -99.83 -99.32 -99.79 -99.23
57335737
lat (x, y) float64 42.25 42.21 42.63 42.59
57345738
* time (time) datetime64[ns] 2014-09-06 2014-09-07 2014-09-08
5735-
lon (x, y) float64 -99.83 -99.32 -99.79 -99.23
5739+
reference_time datetime64[ns] 2014-09-05
57365740
Dimensions without coordinates: x, y
57375741
Data variables:
57385742
precipitation (x, y, time) float64 5.68 9.256 0.7104 ... 7.992 4.615 7.805
@@ -5742,10 +5746,10 @@ def filter_by_attrs(self, **kwargs):
57425746
<xarray.Dataset>
57435747
Dimensions: (time: 3, x: 2, y: 2)
57445748
Coordinates:
5745-
reference_time datetime64[ns] 2014-09-05
5749+
lon (x, y) float64 -99.83 -99.32 -99.79 -99.23
57465750
lat (x, y) float64 42.25 42.21 42.63 42.59
57475751
* time (time) datetime64[ns] 2014-09-06 2014-09-07 2014-09-08
5748-
lon (x, y) float64 -99.83 -99.32 -99.79 -99.23
5752+
reference_time datetime64[ns] 2014-09-05
57495753
Dimensions without coordinates: x, y
57505754
Data variables:
57515755
temperature (x, y, time) float64 29.11 18.2 22.83 ... 18.28 16.15 26.63

xarray/tests/test_testing.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,12 +70,7 @@ def test_assert_allclose(obj1, obj2):
7070
pytest.param(
7171
quantity,
7272
id="pint",
73-
marks=[
74-
pytest.mark.skipif(not has_pint, reason="requires pint"),
75-
pytest.mark.xfail(
76-
reason="inconsistencies in the return value of pint's implementation of eq"
77-
),
78-
],
73+
marks=pytest.mark.skipif(not has_pint, reason="requires pint"),
7974
),
8075
),
8176
)
@@ -115,12 +110,7 @@ def test_assert_duckarray_equal_failing(duckarray, obj1, obj2):
115110
pytest.param(
116111
quantity,
117112
id="pint",
118-
marks=[
119-
pytest.mark.skipif(not has_pint, reason="requires pint"),
120-
pytest.mark.xfail(
121-
reason="inconsistencies in the return value of pint's implementation of eq"
122-
),
123-
],
113+
marks=pytest.mark.skipif(not has_pint, reason="requires pint"),
124114
),
125115
),
126116
)

0 commit comments

Comments
 (0)