Skip to content

Commit

Permalink
Fix 4009 (#4173)
Browse files Browse the repository at this point in the history
* 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 <dcherian@users.noreply.github.com>
  • Loading branch information
johnomotani and dcherian authored Jun 24, 2020
1 parent f281b3b commit 24d755d
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
2 changes: 2 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ New Features

Bug fixes
~~~~~~~~~
- Fix errors combining attrs in :py:func:`open_mfdataset` (:issue:`4009`, :pull:`4173`)
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

0 comments on commit 24d755d

Please sign in to comment.