Skip to content

Commit c07160d

Browse files
authored
keep attrs in reset_index (#4103)
* keep attrs when resetting single index * add dataarray test * modify tests * remove rename * update what's new
1 parent 274bd4b commit c07160d

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

doc/whats-new.rst

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,18 +36,20 @@ Breaking changes
3636

3737
Enhancements
3838
~~~~~~~~~~~~
39-
- Performance improvement of :py:meth:`DataArray.interp` and :py:func:`Dataset.interp`
40-
For orthogonal linear- and nearest-neighbor interpolation, we do 1d-interpolation sequentially
39+
- Performance improvement of :py:meth:`DataArray.interp` and :py:func:`Dataset.interp`
40+
For orthogonal linear- and nearest-neighbor interpolation, we do 1d-interpolation sequentially
4141
rather than interpolating in multidimensional space. (:issue:`2223`)
4242
By `Keisuke Fujii <https://github.com/fujiisoup>`_.
43+
- :py:meth:`DataArray.reset_index` and :py:meth:`Dataset.reset_index` now keep
44+
coordinate attributes (:pull:`4103`). By `Oriol Abril <https://github.com/OriolAbril>`_.
4345

4446
New Features
4547
~~~~~~~~~~~~
4648
- Added :py:meth:`xarray.infer_freq` for extending frequency inferring to CFTime indexes and data (:pull:`4033`).
4749
By `Pascal Bourgault <https://github.com/aulemahal>`_.
4850
- ``chunks='auto'`` is now supported in the ``chunks`` argument of
4951
:py:meth:`Dataset.chunk`. (:issue:`4055`)
50-
By `Andrew Williams <https://github.com/AndrewWilliams3142>`_
52+
By `Andrew Williams <https://github.com/AndrewWilliams3142>`_
5153
- Added :py:func:`xarray.cov` and :py:func:`xarray.corr` (:issue:`3784`, :pull:`3550`, :pull:`4089`).
5254
By `Andrew Williams <https://github.com/AndrewWilliams3142>`_ and `Robin Beer <https://github.com/r-beer>`_.
5355
- 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
7779
By `Stephan Hoyer <https://github.com/shoyer>`_.
7880
- Allow plotting of boolean arrays. (:pull:`3766`)
7981
By `Marek Jacob <https://github.com/MeraX>`_
80-
- Enable using MultiIndex levels as cordinates in 1D and 2D plots (:issue:`3927`).
82+
- Enable using MultiIndex levels as cordinates in 1D and 2D plots (:issue:`3927`).
8183
By `Mathias Hauser <https://github.com/mathause>`_.
8284
- A ``days_in_month`` accessor for :py:class:`xarray.CFTimeIndex`, analogous to
8385
the ``days_in_month`` accessor for a :py:class:`pandas.DatetimeIndex`, which

xarray/core/dataset.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ def split_indexes(
329329
else:
330330
vars_to_remove.append(d)
331331
if not drop:
332-
vars_to_create[str(d) + "_"] = Variable(d, index)
332+
vars_to_create[str(d) + "_"] = Variable(d, index, variables[d].attrs)
333333

334334
for d, levs in dim_levels.items():
335335
index = variables[d].to_index()
@@ -341,7 +341,7 @@ def split_indexes(
341341
if not drop:
342342
for lev in levs:
343343
idx = index.get_level_values(lev)
344-
vars_to_create[idx.name] = Variable(d, idx)
344+
vars_to_create[idx.name] = Variable(d, idx, variables[d].attrs)
345345

346346
new_variables = dict(variables)
347347
for v in set(vars_to_remove):

xarray/tests/test_dataarray.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1830,6 +1830,13 @@ def test_reset_index(self):
18301830
expected = DataArray([1, 2], coords={"x_": ("x", ["a", "b"])}, dims="x")
18311831
assert_identical(array.reset_index("x"), expected)
18321832

1833+
def test_reset_index_keep_attrs(self):
1834+
coord_1 = DataArray([1, 2], dims=["coord_1"], attrs={"attrs": True})
1835+
da = DataArray([1, 0], [coord_1])
1836+
expected = DataArray([1, 0], {"coord_1_": coord_1}, dims=["coord_1"])
1837+
obj = da.reset_index("coord_1")
1838+
assert_identical(expected, obj)
1839+
18331840
def test_reorder_levels(self):
18341841
midx = self.mindex.reorder_levels(["level_2", "level_1"])
18351842
expected = DataArray(self.mda.values, coords={"x": midx}, dims="x")

xarray/tests/test_dataset.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2864,6 +2864,13 @@ def test_reset_index(self):
28642864
with pytest.raises(TypeError):
28652865
ds.reset_index("x", inplace=True)
28662866

2867+
def test_reset_index_keep_attrs(self):
2868+
coord_1 = DataArray([1, 2], dims=["coord_1"], attrs={"attrs": True})
2869+
ds = Dataset({}, {"coord_1": coord_1})
2870+
expected = Dataset({}, {"coord_1_": coord_1})
2871+
obj = ds.reset_index("coord_1")
2872+
assert_identical(expected, obj)
2873+
28672874
def test_reorder_levels(self):
28682875
ds = create_test_multiindex()
28692876
mindex = ds["x"].to_index()

0 commit comments

Comments
 (0)