Skip to content

Commit 63ba862

Browse files
authored
Preserve all attrs with GroupBy by default. (#7022)
1 parent e00e51f commit 63ba862

File tree

3 files changed

+11
-2
lines changed

3 files changed

+11
-2
lines changed

doc/whats-new.rst

+3
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ Bug fixes
6565
By `András Gunyhó <https://github.com/mgunyho>`_.
6666
- Avoid use of random numbers in `test_weighted.test_weighted_operations_nonequal_coords` (:issue:`6504`, :pull:`6961`).
6767
By `Luke Conibear <https://github.com/lukeconibear>`_.
68+
- Use ``keep_attrs=True`` in grouping and resampling operations by default (:issue:`7012`).
69+
This means :py:attr:`Dataset.attrs` and :py:attr:`DataArray.attrs` are now preserved by default.
70+
By `Deepak Cherian <https://github.com/dcherian>`_.
6871
- ``Dataset.encoding['source']`` now exists when reading from a Path object (:issue:`5888`, :pull:`6974`)
6972
By `Thomas Coleman <https://github.com/ColemanTom>`_.
7073

xarray/core/groupby.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -662,7 +662,7 @@ def _flox_reduce(self, dim, keep_attrs=None, **kwargs):
662662
obj = self._original_obj
663663

664664
if keep_attrs is None:
665-
keep_attrs = _get_keep_attrs(default=False)
665+
keep_attrs = _get_keep_attrs(default=True)
666666

667667
# preserve current strategy (approximately) for dask groupby.
668668
# We want to control the default anyway to prevent surprises
@@ -1173,6 +1173,9 @@ def reduce(
11731173
if dim is None:
11741174
dim = self._group_dim
11751175

1176+
if keep_attrs is None:
1177+
keep_attrs = _get_keep_attrs(default=True)
1178+
11761179
def reduce_array(ar: DataArray) -> DataArray:
11771180
return ar.reduce(
11781181
func=func,
@@ -1323,6 +1326,9 @@ def reduce(
13231326
if dim is None:
13241327
dim = self._group_dim
13251328

1329+
if keep_attrs is None:
1330+
keep_attrs = _get_keep_attrs(default=True)
1331+
13261332
def reduce_dataset(ds: Dataset) -> Dataset:
13271333
return ds.reduce(
13281334
func=func,

xarray/tests/test_groupby.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def dataset():
3030
"foo": (("x", "y", "z"), np.random.randn(3, 4, 2)),
3131
"baz": ("x", ["e", "f", "g"]),
3232
},
33-
{"x": ["a", "b", "c"], "y": [1, 2, 3, 4], "z": [1, 2]},
33+
{"x": ("x", ["a", "b", "c"], {"name": "x"}), "y": [1, 2, 3, 4], "z": [1, 2]},
3434
)
3535
ds["boo"] = (("z", "y"), [["f", "g", "h", "j"]] * 2)
3636

0 commit comments

Comments
 (0)