Skip to content

Commit

Permalink
concat_dim for auto_combine for a single object is now respected
Browse files Browse the repository at this point in the history
  • Loading branch information
WeatherGod committed Apr 10, 2018
1 parent 6402391 commit 52c8158
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 1 deletion.
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
16 changes: 16 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -2039,6 +2039,22 @@ 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
20 changes: 20 additions & 0 deletions xarray/tests/test_combine.py
Original file line number Diff line number Diff line change
Expand Up @@ -377,3 +377,23 @@ 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)

0 comments on commit 52c8158

Please sign in to comment.