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

concat_dim for auto_combine for a single object is now respected #2048

Merged
merged 2 commits into from
Apr 10, 2018
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 4 additions & 0 deletions doc/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ Bug fixes
- Fixed a bug in decode_cf_datetime where ``int32`` arrays weren't parsed
correctly (:issue:`2002`).
By `Fabien Maussion <https://github.com/fmaussion>`_.
- When calling `xr.auto_combine()` or `xr.open_mfdataset()` with a `concat_dim`,
the resulting dataset will have that one-element dimension (it was
silently dropped, previously) (:issue:`1988`).
By `Ben Root <https://github.com/WeatherGod>`_.

.. _whats-new.0.10.2:

Expand Down
3 changes: 2 additions & 1 deletion xarray/core/combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,8 @@ def _dataarray_concat(arrays, dim, data_vars, coords, compat,


def _auto_concat(datasets, dim=None, data_vars='all', coords='different'):
if len(datasets) == 1:
if len(datasets) == 1 and dim is None:
# There is nothing more to combine, so kick out early.
return datasets[0]
else:
if dim is None:
Expand Down
14 changes: 14 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -2039,6 +2039,20 @@ def test_open_dataset(self):
self.assertIsInstance(actual.foo.variable.data, np.ndarray)
assert_identical(original, actual)

def test_open_single_dataset(self):
# Test for issue GH #1988. This makes sure that the
# concat_dim is utilized when specified in open_mfdataset().
rnddata = np.random.randn(10)
original = Dataset({'foo': ('x', rnddata)})
dim = DataArray([100], name='baz', dims='baz')
expected = Dataset({'foo': (('baz', 'x'), rnddata[np.newaxis, :])},
{'baz': [100]})
with create_tmp_file() as tmp:
original.to_netcdf(tmp)
with open_mfdataset([tmp], concat_dim=dim,
autoclose=self.autoclose) as actual:
assert_identical(expected, actual)

def test_dask_roundtrip(self):
with create_tmp_file() as tmp:
data = create_test_data()
Expand Down
19 changes: 19 additions & 0 deletions xarray/tests/test_combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,22 @@ def test_auto_combine_no_concat(self):
data = Dataset({'x': 0})
actual = auto_combine([data, data, data], concat_dim=None)
assert_identical(data, actual)

# Single object, with a concat_dim explicitly provided
# Test the issue reported in GH #1988
objs = [Dataset({'x': 0, 'y': 1})]
dim = DataArray([100], name='baz', dims='baz')
actual = auto_combine(objs, concat_dim=dim)
expected = Dataset({'x': ('baz', [0]), 'y': ('baz', [1])},
{'baz': [100]})
assert_identical(expected, actual)

# Just making sure that auto_combine is doing what is
# expected for non-scalar values, too.
objs = [Dataset({'x': ('z', [0, 1]), 'y': ('z', [1, 2])})]
dim = DataArray([100], name='baz', dims='baz')
actual = auto_combine(objs, concat_dim=dim)
expected = Dataset({'x': (('baz', 'z'), [[0, 1]]),
'y': (('baz', 'z'), [[1, 2]])},
{'baz': [100]})
assert_identical(expected, actual)