Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix 4009 #4173

Merged
merged 4 commits into from
Jun 24, 2020
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ New Features

Bug fixes
~~~~~~~~~
- Errors combining attrs in open_mfdataset (:issue:`4009`, :pull:`4173`)
dcherian marked this conversation as resolved.
Show resolved Hide resolved
By `John Omotani <https://github.com/johnomotani>`_
- If groupby receives a ``DataArray`` with name=None, assign a default name (:issue:`158`)
By `Phil Butcher <https://github.com/pjbutcher>`_.
- Support dark mode in VS code (:issue:`4024`)
Expand Down
8 changes: 7 additions & 1 deletion xarray/backends/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
30 changes: 30 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down