Skip to content

Commit 24d755d

Browse files
Fix 4009 (#4173)
* Test attrs handling in open_mfdataset * Fix attrs handling in open_mfdataset() Need to pass combine_attrs="drop", to allow attrs_file to set the attrs. * Update whats-new.rst * Update doc/whats-new.rst Co-authored-by: Deepak Cherian <[email protected]>
1 parent f281b3b commit 24d755d

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

doc/whats-new.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ New Features
107107

108108
Bug fixes
109109
~~~~~~~~~
110+
- Fix errors combining attrs in :py:func:`open_mfdataset` (:issue:`4009`, :pull:`4173`)
111+
By `John Omotani <https://github.com/johnomotani>`_
110112
- If groupby receives a ``DataArray`` with name=None, assign a default name (:issue:`158`)
111113
By `Phil Butcher <https://github.com/pjbutcher>`_.
112114
- Support dark mode in VS code (:issue:`4024`)

xarray/backends/api.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -967,12 +967,18 @@ def open_mfdataset(
967967
coords=coords,
968968
ids=ids,
969969
join=join,
970+
combine_attrs="drop",
970971
)
971972
elif combine == "by_coords":
972973
# Redo ordering from coordinates, ignoring how they were ordered
973974
# previously
974975
combined = combine_by_coords(
975-
datasets, compat=compat, data_vars=data_vars, coords=coords, join=join
976+
datasets,
977+
compat=compat,
978+
data_vars=data_vars,
979+
coords=coords,
980+
join=join,
981+
combine_attrs="drop",
976982
)
977983
else:
978984
raise ValueError(

xarray/tests/test_backends.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2662,6 +2662,36 @@ def test_open_mfdataset_does_same_as_concat(self, combine, opt, join):
26622662
ds_expect = xr.concat([ds1, ds2], data_vars=opt, dim="t", join=join)
26632663
assert_identical(ds, ds_expect)
26642664

2665+
def test_open_mfdataset_dataset_attr_by_coords(self):
2666+
"""
2667+
Case when an attribute differs across the multiple files
2668+
"""
2669+
with self.setup_files_and_datasets() as (files, [ds1, ds2]):
2670+
# Give the files an inconsistent attribute
2671+
for i, f in enumerate(files):
2672+
ds = open_dataset(f).load()
2673+
ds.attrs["test_dataset_attr"] = 10 + i
2674+
ds.close()
2675+
ds.to_netcdf(f)
2676+
2677+
with xr.open_mfdataset(files, combine="by_coords", concat_dim="t") as ds:
2678+
assert ds.test_dataset_attr == 10
2679+
2680+
def test_open_mfdataset_dataarray_attr_by_coords(self):
2681+
"""
2682+
Case when an attribute of a member DataArray differs across the multiple files
2683+
"""
2684+
with self.setup_files_and_datasets() as (files, [ds1, ds2]):
2685+
# Give the files an inconsistent attribute
2686+
for i, f in enumerate(files):
2687+
ds = open_dataset(f).load()
2688+
ds["v1"].attrs["test_dataarray_attr"] = i
2689+
ds.close()
2690+
ds.to_netcdf(f)
2691+
2692+
with xr.open_mfdataset(files, combine="by_coords", concat_dim="t") as ds:
2693+
assert ds["v1"].test_dataarray_attr == 0
2694+
26652695
@pytest.mark.parametrize("combine", ["nested", "by_coords"])
26662696
@pytest.mark.parametrize("opt", ["all", "minimal", "different"])
26672697
def test_open_mfdataset_exact_join_raises_error(self, combine, opt):

0 commit comments

Comments
 (0)