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
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
22 changes: 16 additions & 6 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -1123,7 +1123,13 @@ def _copy_listed(self, names: Iterable[Hashable]) -> "Dataset":
coord_names = set()
indexes: Dict[Hashable, pd.Index] = {}

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

for name in sorted(names, key=key):
keewis marked this conversation as resolved.
Show resolved Hide resolved
try:
variables[name] = self._variables[name]
except KeyError:
Expand All @@ -1142,7 +1148,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 +5739,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 +5752,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