From 3415e63d1c26565c1b554efd126783dae76e9d97 Mon Sep 17 00:00:00 2001 From: "Oriol (Prodesk)" Date: Thu, 28 May 2020 06:41:27 +0200 Subject: [PATCH 1/5] keep attrs when resetting single index --- xarray/core/dataset.py | 4 ++-- xarray/tests/test_dataset.py | 6 ++++++ 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index 2d0044711fe..3bd6ea44e6a 100644 --- a/xarray/core/dataset.py +++ b/xarray/core/dataset.py @@ -329,7 +329,7 @@ def split_indexes( else: vars_to_remove.append(d) if not drop: - vars_to_create[str(d) + "_"] = Variable(d, index) + vars_to_create[str(d) + "_"] = Variable(d, index, variables[d].attrs) for d, levs in dim_levels.items(): index = variables[d].to_index() @@ -341,7 +341,7 @@ def split_indexes( if not drop: for lev in levs: idx = index.get_level_values(lev) - vars_to_create[idx.name] = Variable(d, idx) + vars_to_create[idx.name] = Variable(d, idx, variables[d].attrs) new_variables = dict(variables) for v in set(vars_to_remove): diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 2a89920766c..0c62dc8be71 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -2864,6 +2864,12 @@ def test_reset_index(self): with pytest.raises(TypeError): ds.reset_index("x", inplace=True) + def test_reset_index_keep_attrs(self): + coord_1 = xr.DataArray([1, 2], dims=["coord_1"], attrs={"attrs": True}) + ds = xr.Dataset({}, {"coord_1": coord_1}) + obj = ds.reset_index("coord_1").rename({"coord_1_": "coord_1"}) + assert_identical(ds, obj) + def test_reorder_levels(self): ds = create_test_multiindex() mindex = ds["x"].to_index() From 225981bed6237f342712bf4b4e8ee983689de933 Mon Sep 17 00:00:00 2001 From: "Oriol (Prodesk)" Date: Thu, 28 May 2020 06:59:44 +0200 Subject: [PATCH 2/5] add dataarray test --- xarray/tests/test_dataarray.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 54a77261fb4..25f47eb7503 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -1830,6 +1830,12 @@ def test_reset_index(self): expected = DataArray([1, 2], coords={"x_": ("x", ["a", "b"])}, dims="x") assert_identical(array.reset_index("x"), expected) + def test_reset_index_keep_attrs(self): + coord_1 = xr.DataArray([1, 2], dims=["coord_1"], attrs={"attrs": True}) + da = xr.DataArray([1, 0], [coord_1]) + obj = da.reset_index("coord_1").rename({"coord_1_": "coord_1"}) + assert_identical(da, obj) + def test_reorder_levels(self): midx = self.mindex.reorder_levels(["level_2", "level_1"]) expected = DataArray(self.mda.values, coords={"x": midx}, dims="x") From 68095942468797f177735dc3205409c1701bf4f6 Mon Sep 17 00:00:00 2001 From: "Oriol (Prodesk)" Date: Thu, 28 May 2020 22:10:26 +0200 Subject: [PATCH 3/5] modify tests --- xarray/tests/test_dataarray.py | 9 +++++---- xarray/tests/test_dataset.py | 7 ++++--- 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/xarray/tests/test_dataarray.py b/xarray/tests/test_dataarray.py index 25f47eb7503..95f0ad9f612 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -1831,10 +1831,11 @@ def test_reset_index(self): assert_identical(array.reset_index("x"), expected) def test_reset_index_keep_attrs(self): - coord_1 = xr.DataArray([1, 2], dims=["coord_1"], attrs={"attrs": True}) - da = xr.DataArray([1, 0], [coord_1]) - obj = da.reset_index("coord_1").rename({"coord_1_": "coord_1"}) - assert_identical(da, obj) + coord_1 = DataArray([1, 2], dims=["coord_1"], attrs={"attrs": True}) + da = DataArray([1, 0], [coord_1]) + expected = DataArray([1, 0], {"coord_1_": coord_1}, dims=["coord_1"]) + obj = da.reset_index("coord_1") + assert_identical(expected, obj) def test_reorder_levels(self): midx = self.mindex.reorder_levels(["level_2", "level_1"]) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 0c62dc8be71..b292f2b58c8 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -2865,10 +2865,11 @@ def test_reset_index(self): ds.reset_index("x", inplace=True) def test_reset_index_keep_attrs(self): - coord_1 = xr.DataArray([1, 2], dims=["coord_1"], attrs={"attrs": True}) - ds = xr.Dataset({}, {"coord_1": coord_1}) + coord_1 = DataArray([1, 2], dims=["coord_1"], attrs={"attrs": True}) + ds = Dataset({}, {"coord_1": coord_1}) + expected = Dataset({}, {"coord_1_": coord_1}) obj = ds.reset_index("coord_1").rename({"coord_1_": "coord_1"}) - assert_identical(ds, obj) + assert_identical(expected, obj) def test_reorder_levels(self): ds = create_test_multiindex() From 9bf6faf42949bc3613298b86c90513449b9fb8cd Mon Sep 17 00:00:00 2001 From: "Oriol (Prodesk)" Date: Thu, 28 May 2020 22:18:14 +0200 Subject: [PATCH 4/5] remove rename --- xarray/tests/test_dataset.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index b292f2b58c8..fd04c8a7f64 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -2868,7 +2868,7 @@ def test_reset_index_keep_attrs(self): coord_1 = DataArray([1, 2], dims=["coord_1"], attrs={"attrs": True}) ds = Dataset({}, {"coord_1": coord_1}) expected = Dataset({}, {"coord_1_": coord_1}) - obj = ds.reset_index("coord_1").rename({"coord_1_": "coord_1"}) + obj = ds.reset_index("coord_1") assert_identical(expected, obj) def test_reorder_levels(self): From f8f0ecb5e27c1e7b868cd0f8078320957eb32464 Mon Sep 17 00:00:00 2001 From: "Oriol (Prodesk)" Date: Fri, 29 May 2020 19:53:07 +0200 Subject: [PATCH 5/5] update what's new --- doc/whats-new.rst | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index a32e0393bcf..98c7713fdc0 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -36,17 +36,19 @@ Breaking changes Enhancements ~~~~~~~~~~~~ -- Performance improvement of :py:meth:`DataArray.interp` and :py:func:`Dataset.interp` - For orthogonal linear- and nearest-neighbor interpolation, we do 1d-interpolation sequentially +- Performance improvement of :py:meth:`DataArray.interp` and :py:func:`Dataset.interp` + For orthogonal linear- and nearest-neighbor interpolation, we do 1d-interpolation sequentially rather than interpolating in multidimensional space. (:issue:`2223`) By `Keisuke Fujii `_. +- :py:meth:`DataArray.reset_index` and :py:meth:`Dataset.reset_index` now keep + coordinate attributes (:pull:`4103`). By `Oriol Abril `_. New Features ~~~~~~~~~~~~ - ``chunks='auto'`` is now supported in the ``chunks`` argument of :py:meth:`Dataset.chunk`. (:issue:`4055`) - By `Andrew Williams `_ + By `Andrew Williams `_ - Added :py:func:`xarray.cov` and :py:func:`xarray.corr` (:issue:`3784`, :pull:`3550`, :pull:`4089`). By `Andrew Williams `_ and `Robin Beer `_. - Added :py:meth:`DataArray.polyfit` and :py:func:`xarray.polyval` for fitting polynomials. (:issue:`3349`) @@ -76,7 +78,7 @@ New Features By `Stephan Hoyer `_. - Allow plotting of boolean arrays. (:pull:`3766`) By `Marek Jacob `_ -- Enable using MultiIndex levels as cordinates in 1D and 2D plots (:issue:`3927`). +- Enable using MultiIndex levels as cordinates in 1D and 2D plots (:issue:`3927`). By `Mathias Hauser `_. - A ``days_in_month`` accessor for :py:class:`xarray.CFTimeIndex`, analogous to the ``days_in_month`` accessor for a :py:class:`pandas.DatetimeIndex`, which