Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Keep the original ordering of the coordinates #4409

Merged
merged 12 commits into from
Sep 18, 2020
2 changes: 1 addition & 1 deletion ci/requirements/py36-min-nep18.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies:
- distributed=2.9
- numpy=1.17
- pandas=0.25
- pint=0.13
- pint=0.15
- pip
- pytest
- pytest-cov
Expand Down
8 changes: 6 additions & 2 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ New Features
now accept more than 1 dimension. (:pull:`4219`)
By `Keisuke Fujii <https://github.com/fujiisoup>`_.
- ``min_count`` can be supplied to reductions such as ``.sum`` when specifying
multiple dimension to reduce over. (:pull:`4356`)
multiple dimension to reduce over. (:pull:`4356`)
By `Maximilian Roos <https://github.com/max-sixty>`_.
- :py:func:`xarray.cov` and :py:func:`xarray.corr` now handle missing values. (:pull:`4351`)
By `Maximilian Roos <https://github.com/max-sixty>`_.
Expand Down Expand Up @@ -77,7 +77,7 @@ Bug fixes
and :py:meth:`DataArray.str.wrap` (:issue:`4334`). By `Mathias Hauser <https://github.com/mathause>`_.
- Fixed overflow issue causing incorrect results in computing means of :py:class:`cftime.datetime`
arrays (:issue:`4341`). By `Spencer Clark <https://github.com/spencerkclark>`_.
- 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>`_.
- 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>`_.
- fix the signature of the plot methods. (:pull:`4359`) By `Justus Magin <https://github.com/keewis>`_.
- Fix :py:func:`xarray.apply_ufunc` with ``vectorize=True`` and ``exclude_dims`` (:issue:`3890`).
By `Mathias Hauser <https://github.com/mathause>`_.
Expand All @@ -86,6 +86,8 @@ Bug fixes
By `Jens Svensmark <https://github.com/jenssss>`_
- Fix incorrect legend labels for :py:meth:`Dataset.plot.scatter` (:issue:`4126`).
By `Peter Hausamann <https://github.com/phausamann>`_.
- Avoid relying on :py:class:`set` objects for the ordering of the coordinates (:pull:`4409`)
By `Justus Magin <https://github.com/keewis>`_.

Documentation
~~~~~~~~~~~~~
Expand All @@ -96,6 +98,8 @@ Documentation
By `Sander van Rijn <https://github.com/sjvrijn>`_
- Update the contributing guide to use merges instead of rebasing and state
that we squash-merge. (:pull:`4355`) By `Justus Magin <https://github.com/keewis>`_.
- Make sure the examples from the docstrings actually work (:pull:`4408`).
By `Justus Magin <https://github.com/keewis>`_.

Internal Changes
~~~~~~~~~~~~~~~~
Expand Down
6 changes: 5 additions & 1 deletion xarray/core/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,11 @@ def _parse_datasets(
all_coord_names.update(ds.coords)
data_vars.update(ds.data_vars)

for dim in set(ds.dims) - dims:
# preserves ordering of dimensions
for dim in ds.dims:
keewis marked this conversation as resolved.
Show resolved Hide resolved
if dim in dims:
continue

if dim not in dim_coords:
dim_coords[dim] = ds.coords[dim].variable
dims = dims | set(ds.dims)
Expand Down
10 changes: 9 additions & 1 deletion xarray/core/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,15 @@ def __getitem__(self, key: Hashable) -> "DataArray":

def to_dataset(self) -> "Dataset":
"""Convert these coordinates into a new Dataset"""
return self._data._copy_listed(self._names)

def key(name):
try:
return list(self._data._variables.keys()).index(name)
except ValueError:
return len(self._data._variables)

names = sorted(self._names, key=key)
keewis marked this conversation as resolved.
Show resolved Hide resolved
return self._data._copy_listed(names)

def _update_coords(
self, coords: Dict[Hashable, Variable], indexes: Mapping[Hashable, pd.Index]
Expand Down
14 changes: 9 additions & 5 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,11 @@ def _copy_listed(self, names: Iterable[Hashable]) -> "Dataset":

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

for k in self._coord_names:
# preserves ordering of coordinates
for k in self._variables:
keewis marked this conversation as resolved.
Show resolved Hide resolved
if k not in self._coord_names:
continue

if set(self.variables[k].dims) <= needed_dims:
variables[k] = self._variables[k]
coord_names.add(k)
Expand Down Expand Up @@ -5729,10 +5733,10 @@ def filter_by_attrs(self, **kwargs):
<xarray.Dataset>
Dimensions: (time: 3, x: 2, y: 2)
Coordinates:
reference_time datetime64[ns] 2014-09-05
lon (x, y) float64 -99.83 -99.32 -99.79 -99.23
lat (x, y) float64 42.25 42.21 42.63 42.59
* time (time) datetime64[ns] 2014-09-06 2014-09-07 2014-09-08
lon (x, y) float64 -99.83 -99.32 -99.79 -99.23
reference_time datetime64[ns] 2014-09-05
Dimensions without coordinates: x, y
Data variables:
precipitation (x, y, time) float64 5.68 9.256 0.7104 ... 7.992 4.615 7.805
Expand All @@ -5742,10 +5746,10 @@ def filter_by_attrs(self, **kwargs):
<xarray.Dataset>
Dimensions: (time: 3, x: 2, y: 2)
Coordinates:
reference_time datetime64[ns] 2014-09-05
lon (x, y) float64 -99.83 -99.32 -99.79 -99.23
lat (x, y) float64 42.25 42.21 42.63 42.59
* time (time) datetime64[ns] 2014-09-06 2014-09-07 2014-09-08
lon (x, y) float64 -99.83 -99.32 -99.79 -99.23
reference_time datetime64[ns] 2014-09-05
Dimensions without coordinates: x, y
Data variables:
temperature (x, y, time) float64 29.11 18.2 22.83 ... 18.28 16.15 26.63
Expand Down
14 changes: 2 additions & 12 deletions xarray/tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,7 @@ def test_assert_allclose(obj1, obj2):
pytest.param(
quantity,
id="pint",
marks=[
pytest.mark.skipif(not has_pint, reason="requires pint"),
pytest.mark.xfail(
reason="inconsistencies in the return value of pint's implementation of eq"
),
],
marks=pytest.mark.skipif(not has_pint, reason="requires pint"),
),
),
)
Expand Down Expand Up @@ -115,12 +110,7 @@ def test_assert_duckarray_equal_failing(duckarray, obj1, obj2):
pytest.param(
quantity,
id="pint",
marks=[
pytest.mark.skipif(not has_pint, reason="requires pint"),
pytest.mark.xfail(
reason="inconsistencies in the return value of pint's implementation of eq"
),
],
marks=pytest.mark.skipif(not has_pint, reason="requires pint"),
),
),
)
Expand Down