diff --git a/doc/whats-new.rst b/doc/whats-new.rst index 4b5bb1e491f..6a6934e25c5 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -105,6 +105,8 @@ New Features Bug fixes ~~~~~~~~~ +- Fix errors combining attrs in :py:func:`open_mfdataset` (:issue:`4009`, :pull:`4173`) + By `John Omotani `_ - If groupby receives a ``DataArray`` with name=None, assign a default name (:issue:`158`) By `Phil Butcher `_. - Support dark mode in VS code (:issue:`4024`) diff --git a/xarray/backends/api.py b/xarray/backends/api.py index 0919d2a582b..4077d7a02c8 100644 --- a/xarray/backends/api.py +++ b/xarray/backends/api.py @@ -967,12 +967,18 @@ def open_mfdataset( coords=coords, ids=ids, join=join, + combine_attrs="drop", ) elif combine == "by_coords": # Redo ordering from coordinates, ignoring how they were ordered # previously combined = combine_by_coords( - datasets, compat=compat, data_vars=data_vars, coords=coords, join=join + datasets, + compat=compat, + data_vars=data_vars, + coords=coords, + join=join, + combine_attrs="drop", ) else: raise ValueError( diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 177435fa864..1e33eccb83e 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -2662,6 +2662,36 @@ def test_open_mfdataset_does_same_as_concat(self, combine, opt, join): ds_expect = xr.concat([ds1, ds2], data_vars=opt, dim="t", join=join) assert_identical(ds, ds_expect) + def test_open_mfdataset_dataset_attr_by_coords(self): + """ + Case when an attribute differs across the multiple files + """ + with self.setup_files_and_datasets() as (files, [ds1, ds2]): + # Give the files an inconsistent attribute + for i, f in enumerate(files): + ds = open_dataset(f).load() + ds.attrs["test_dataset_attr"] = 10 + i + ds.close() + ds.to_netcdf(f) + + with xr.open_mfdataset(files, combine="by_coords", concat_dim="t") as ds: + assert ds.test_dataset_attr == 10 + + def test_open_mfdataset_dataarray_attr_by_coords(self): + """ + Case when an attribute of a member DataArray differs across the multiple files + """ + with self.setup_files_and_datasets() as (files, [ds1, ds2]): + # Give the files an inconsistent attribute + for i, f in enumerate(files): + ds = open_dataset(f).load() + ds["v1"].attrs["test_dataarray_attr"] = i + ds.close() + ds.to_netcdf(f) + + with xr.open_mfdataset(files, combine="by_coords", concat_dim="t") as ds: + assert ds["v1"].test_dataarray_attr == 0 + @pytest.mark.parametrize("combine", ["nested", "by_coords"]) @pytest.mark.parametrize("opt", ["all", "minimal", "different"]) def test_open_mfdataset_exact_join_raises_error(self, combine, opt):