| 2758 | assert_identical(actual7, expected6) |
| 2759 | |
| 2760 | def test_stack_unstack(self) -> None: |
| 2761 | orig = DataArray( |
| 2762 | [[0, 1], [2, 3]], |
| 2763 | dims=["x", "y"], |
| 2764 | attrs={"foo": 2}, |
| 2765 | ) |
| 2766 | assert_identical(orig, orig.unstack()) |
| 2767 | |
| 2768 | # test GH3000 |
| 2769 | a = orig[:0, :1].stack(new_dim=("x", "y")).indexes["new_dim"] |
| 2770 | b = pd.MultiIndex( |
| 2771 | levels=[ |
| 2772 | pd.Index([], dtype=np.int64), # type: ignore[list-item,unused-ignore] |
| 2773 | pd.Index([0], dtype=np.int64), # type: ignore[list-item,unused-ignore] |
| 2774 | ], |
| 2775 | codes=[[], []], |
| 2776 | names=["x", "y"], |
| 2777 | ) |
| 2778 | pd.testing.assert_index_equal(a, b) |
| 2779 | |
| 2780 | actual = orig.stack(z=["x", "y"]).unstack("z").drop_vars(["x", "y"]) |
| 2781 | assert_identical(orig, actual) |
| 2782 | |
| 2783 | actual = orig.stack(z=[...]).unstack("z").drop_vars(["x", "y"]) |
| 2784 | assert_identical(orig, actual) |
| 2785 | |
| 2786 | dims = ["a", "b", "c", "d", "e"] |
| 2787 | coords = { |
| 2788 | "a": [0], |
| 2789 | "b": [1, 2], |
| 2790 | "c": [3, 4, 5], |
| 2791 | "d": [6, 7], |
| 2792 | "e": [8], |
| 2793 | } |
| 2794 | orig = xr.DataArray(np.random.rand(1, 2, 3, 2, 1), coords=coords, dims=dims) |
| 2795 | stacked = orig.stack(ab=["a", "b"], cd=["c", "d"]) |
| 2796 | |
| 2797 | unstacked = stacked.unstack(["ab", "cd"]) |
| 2798 | assert_identical(orig, unstacked.transpose(*dims)) |
| 2799 | |
| 2800 | unstacked = stacked.unstack() |
| 2801 | assert_identical(orig, unstacked.transpose(*dims)) |
| 2802 | |
| 2803 | def test_stack_unstack_decreasing_coordinate(self) -> None: |
| 2804 | # regression test for GH980 |