diff --git a/doc/whats-new.rst b/doc/whats-new.rst index b3130a1a4ab..fcfe4887485 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -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 `_. +- 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 `_. .. _whats-new.0.10.2: diff --git a/xarray/core/combine.py b/xarray/core/combine.py index 8c1c58e9a40..430f0e564d6 100644 --- a/xarray/core/combine.py +++ b/xarray/core/combine.py @@ -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: diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index e0f030368bb..a58104deef5 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -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() diff --git a/xarray/tests/test_combine.py b/xarray/tests/test_combine.py index 09918d9a065..482a280b355 100644 --- a/xarray/tests/test_combine.py +++ b/xarray/tests/test_combine.py @@ -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)