diff --git a/xarray/core/concat.py b/xarray/core/concat.py index b23073e7e70..f4af256430c 100644 --- a/xarray/core/concat.py +++ b/xarray/core/concat.py @@ -532,7 +532,7 @@ def get_indexes(name): elif name == dim: var = ds._variables[name] if not var.dims: - yield PandasIndex([var.values.item()], dim) + yield PandasIndex([var.values.item()], dim, coord_dtype=var.dtype) # stack up each variable and/or index to fill-out the dataset (in order) # n.b. this loop preserves variable order, needed for groupby. diff --git a/xarray/tests/test_concat.py b/xarray/tests/test_concat.py index f31b6972187..c0b98e28b12 100644 --- a/xarray/tests/test_concat.py +++ b/xarray/tests/test_concat.py @@ -456,6 +456,17 @@ def test_concat_promote_shape(self) -> None: ) assert_identical(actual, expected) + # regression GH6416 (coord dtype) + time_data1 = np.array(["2022-01-01", "2022-02-01"], dtype="datetime64[ns]") + time_data2 = np.array("2022-03-01", dtype="datetime64[ns]") + time_expected = np.array( + ["2022-01-01", "2022-02-01", "2022-03-01"], dtype="datetime64[ns]" + ) + objs = [Dataset({}, {"time": time_data1}), Dataset({}, {"time": time_data2})] + actual = concat(objs, "time") + expected = Dataset({}, {"time": time_expected}) + assert_identical(actual, expected) + def test_concat_do_not_promote(self) -> None: # GH438 objs = [