Skip to content

Commit 6057525

Browse files
committed
concat_dim for auto_combine for a single object is now respected
Closes #1988
1 parent 6402391 commit 6057525

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

xarray/core/combine.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -340,7 +340,8 @@ def _dataarray_concat(arrays, dim, data_vars, coords, compat,
340340

341341

342342
def _auto_concat(datasets, dim=None, data_vars='all', coords='different'):
343-
if len(datasets) == 1:
343+
if len(datasets) == 1 and dim is None:
344+
# There is nothing more to combine, so kick out early.
344345
return datasets[0]
345346
else:
346347
if dim is None:

xarray/tests/test_backends.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,6 +2039,20 @@ def test_open_dataset(self):
20392039
self.assertIsInstance(actual.foo.variable.data, np.ndarray)
20402040
assert_identical(original, actual)
20412041

2042+
def test_open_single_dataset(self):
2043+
# Test for issue GH #1988. This makes sure that the
2044+
# concat_dim is utilized when specified in open_mfdataset().
2045+
rnddata = np.random.randn(10)
2046+
original = Dataset({'foo': ('x', rnddata)})
2047+
dim = DataArray([100], name='baz', dims='baz')
2048+
expected = Dataset({'foo': (('baz', 'x'), rnddata[np.newaxis, :])},
2049+
{'baz': [100]})
2050+
with create_tmp_file() as tmp:
2051+
original.to_netcdf(tmp)
2052+
with open_mfdataset([tmp], concat_dim=dim,
2053+
autoclose=self.autoclose) as actual:
2054+
assert_identical(expected, actual)
2055+
20422056
def test_dask_roundtrip(self):
20432057
with create_tmp_file() as tmp:
20442058
data = create_test_data()

xarray/tests/test_combine.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -377,3 +377,22 @@ def test_auto_combine_no_concat(self):
377377
data = Dataset({'x': 0})
378378
actual = auto_combine([data, data, data], concat_dim=None)
379379
assert_identical(data, actual)
380+
381+
# Single object, with a concat_dim explicitly provided
382+
# Test the issue reported in GH #1988
383+
objs = [Dataset({'x': 0, 'y': 1})]
384+
dim = DataArray([100], name='baz', dims='baz')
385+
actual = auto_combine(objs, concat_dim=dim)
386+
expected = Dataset({'x': ('baz', [0]), 'y': ('baz', [1])},
387+
{'baz': [100]})
388+
assert_identical(expected, actual)
389+
390+
# Just making sure that auto_combine is doing what is
391+
# expected for non-scalar values, too.
392+
objs = [Dataset({'x': ('z', [0, 1]), 'y': ('z', [1, 2])})]
393+
dim = DataArray([100], name='baz', dims='baz')
394+
actual = auto_combine(objs, concat_dim=dim)
395+
expected = Dataset({'x': (('baz', 'z'), [[0, 1]]),
396+
'y': (('baz', 'z'), [[1, 2]])},
397+
{'baz': [100]})
398+
assert_identical(expected, actual)

0 commit comments

Comments
 (0)