|
8 | 8 | import pandas as pd
|
9 | 9 | import pytest
|
10 | 10 |
|
11 |
| -from xarray import DataArray, Dataset, Variable, concat |
| 11 | +from xarray import AlignmentError, DataArray, Dataset, Variable, concat |
12 | 12 | from xarray.core import dtypes
|
13 | 13 | from xarray.core.coordinates import Coordinates
|
14 | 14 | from xarray.core.indexes import PandasIndex
|
|
22 | 22 | assert_identical,
|
23 | 23 | requires_dask,
|
24 | 24 | )
|
| 25 | +from xarray.tests.indexes import XYIndex |
25 | 26 | from xarray.tests.test_dataset import create_test_data
|
26 | 27 |
|
27 | 28 | if TYPE_CHECKING:
|
@@ -1379,3 +1380,48 @@ def test_concat_index_not_same_dim() -> None:
|
1379 | 1380 | match=r"Cannot concatenate along dimension 'x' indexes with dimensions.*",
|
1380 | 1381 | ):
|
1381 | 1382 | concat([ds1, ds2], dim="x")
|
| 1383 | + |
| 1384 | + |
| 1385 | +def test_concat_multi_dim_index() -> None: |
| 1386 | + ds1 = ( |
| 1387 | + Dataset( |
| 1388 | + {"foo": (("x", "y"), np.random.randn(2, 2))}, |
| 1389 | + coords={"x": [1, 2], "y": [3, 4]}, |
| 1390 | + ) |
| 1391 | + .drop_indexes(["x", "y"]) |
| 1392 | + .set_xindex(["x", "y"], XYIndex) |
| 1393 | + ) |
| 1394 | + ds2 = ( |
| 1395 | + Dataset( |
| 1396 | + {"foo": (("x", "y"), np.random.randn(2, 2))}, |
| 1397 | + coords={"x": [1, 2], "y": [5, 6]}, |
| 1398 | + ) |
| 1399 | + .drop_indexes(["x", "y"]) |
| 1400 | + .set_xindex(["x", "y"], XYIndex) |
| 1401 | + ) |
| 1402 | + |
| 1403 | + expected = ( |
| 1404 | + Dataset( |
| 1405 | + { |
| 1406 | + "foo": ( |
| 1407 | + ("x", "y"), |
| 1408 | + np.concatenate([ds1.foo.data, ds2.foo.data], axis=-1), |
| 1409 | + ) |
| 1410 | + }, |
| 1411 | + coords={"x": [1, 2], "y": [3, 4, 5, 6]}, |
| 1412 | + ) |
| 1413 | + .drop_indexes(["x", "y"]) |
| 1414 | + .set_xindex(["x", "y"], XYIndex) |
| 1415 | + ) |
| 1416 | + # note: missing 'override' |
| 1417 | + for join in ["inner", "outer", "exact", "left", "right"]: |
| 1418 | + actual = concat([ds1, ds2], dim="y", join=join) |
| 1419 | + assert_identical(actual, expected, check_default_indexes=False) |
| 1420 | + |
| 1421 | + with pytest.raises(AlignmentError): |
| 1422 | + actual = concat([ds1, ds2], dim="x", join="exact") |
| 1423 | + |
| 1424 | + # TODO: fix these, or raise better error message |
| 1425 | + with pytest.raises(AssertionError): |
| 1426 | + for join in ["left", "right"]: |
| 1427 | + actual = concat([ds1, ds2], dim="x", join=join) |
0 commit comments