From c07160dd2d627a021e58515cbd7753c11fb56d94 Mon Sep 17 00:00:00 2001 From: Oriol Abril Date: Fri, 5 Jun 2020 21:39:09 +0200 Subject: [PATCH] keep attrs in reset_index (#4103) * keep attrs when resetting single index * add dataarray test * modify tests * remove rename * update what's new --- doc/whats-new.rst | 10 ++++++---- xarray/core/dataset.py | 4 ++-- xarray/tests/test_dataarray.py | 7 +++++++ xarray/tests/test_dataset.py | 7 +++++++ 4 files changed, 22 insertions(+), 6 deletions(-) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 0c5e61addf5..21eb28130c2 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -36,10 +36,12 @@ 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 ~~~~~~~~~~~~ @@ -47,7 +49,7 @@ New Features By `Pascal Bourgault `_. - ``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`, :pull:`3733`, :pull:`4099`) @@ -77,7 +79,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 diff --git a/xarray/core/dataset.py b/xarray/core/dataset.py index d50c6e1951e..191b57a667a 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_dataarray.py b/xarray/tests/test_dataarray.py index 54a77261fb4..95f0ad9f612 100644 --- a/xarray/tests/test_dataarray.py +++ b/xarray/tests/test_dataarray.py @@ -1830,6 +1830,13 @@ 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 = 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"]) expected = DataArray(self.mda.values, coords={"x": midx}, dims="x") diff --git a/xarray/tests/test_dataset.py b/xarray/tests/test_dataset.py index 2a89920766c..fd04c8a7f64 100644 --- a/xarray/tests/test_dataset.py +++ b/xarray/tests/test_dataset.py @@ -2864,6 +2864,13 @@ def test_reset_index(self): with pytest.raises(TypeError): ds.reset_index("x", inplace=True) + 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") + assert_identical(expected, obj) + def test_reorder_levels(self): ds = create_test_multiindex() mindex = ds["x"].to_index()