Skip to content

Commit

Permalink
keep attrs in reset_index (#4103)
Browse files Browse the repository at this point in the history
* keep attrs when resetting single index

* add dataarray test

* modify tests

* remove rename

* update what's new
  • Loading branch information
OriolAbril authored Jun 5, 2020
1 parent 274bd4b commit c07160d
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
10 changes: 6 additions & 4 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,20 @@ 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 <https://github.com/fujiisoup>`_.
- :py:meth:`DataArray.reset_index` and :py:meth:`Dataset.reset_index` now keep
coordinate attributes (:pull:`4103`). By `Oriol Abril <https://github.com/OriolAbril>`_.

New Features
~~~~~~~~~~~~
- Added :py:meth:`xarray.infer_freq` for extending frequency inferring to CFTime indexes and data (:pull:`4033`).
By `Pascal Bourgault <https://github.com/aulemahal>`_.
- ``chunks='auto'`` is now supported in the ``chunks`` argument of
:py:meth:`Dataset.chunk`. (:issue:`4055`)
By `Andrew Williams <https://github.com/AndrewWilliams3142>`_
By `Andrew Williams <https://github.com/AndrewWilliams3142>`_
- Added :py:func:`xarray.cov` and :py:func:`xarray.corr` (:issue:`3784`, :pull:`3550`, :pull:`4089`).
By `Andrew Williams <https://github.com/AndrewWilliams3142>`_ and `Robin Beer <https://github.com/r-beer>`_.
- Added :py:meth:`DataArray.polyfit` and :py:func:`xarray.polyval` for fitting polynomials. (:issue:`3349`, :pull:`3733`, :pull:`4099`)
Expand Down Expand Up @@ -77,7 +79,7 @@ New Features
By `Stephan Hoyer <https://github.com/shoyer>`_.
- Allow plotting of boolean arrays. (:pull:`3766`)
By `Marek Jacob <https://github.com/MeraX>`_
- 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 <https://github.com/mathause>`_.
- A ``days_in_month`` accessor for :py:class:`xarray.CFTimeIndex`, analogous to
the ``days_in_month`` accessor for a :py:class:`pandas.DatetimeIndex`, which
Expand Down
4 changes: 2 additions & 2 deletions xarray/core/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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):
Expand Down
7 changes: 7 additions & 0 deletions xarray/tests/test_dataarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
7 changes: 7 additions & 0 deletions xarray/tests/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit c07160d

Please sign in to comment.