(self)
| 6747 | assert_identical(actual, ds) |
| 6748 | |
| 6749 | def test_dataset_transpose(self) -> None: |
| 6750 | ds = Dataset( |
| 6751 | { |
| 6752 | "a": (("x", "y"), np.random.randn(3, 4)), |
| 6753 | "b": (("y", "x"), np.random.randn(4, 3)), |
| 6754 | }, |
| 6755 | coords={ |
| 6756 | "x": range(3), |
| 6757 | "y": range(4), |
| 6758 | "xy": (("x", "y"), np.random.randn(3, 4)), |
| 6759 | }, |
| 6760 | ) |
| 6761 | |
| 6762 | actual = ds.transpose() |
| 6763 | expected = Dataset( |
| 6764 | {"a": (("y", "x"), ds.a.values.T), "b": (("x", "y"), ds.b.values.T)}, |
| 6765 | coords={ |
| 6766 | "x": ds.x.values, |
| 6767 | "y": ds.y.values, |
| 6768 | "xy": (("y", "x"), ds.xy.values.T), |
| 6769 | }, |
| 6770 | ) |
| 6771 | assert_identical(expected, actual) |
| 6772 | |
| 6773 | actual = ds.transpose(...) |
| 6774 | expected = ds |
| 6775 | assert_identical(expected, actual) |
| 6776 | |
| 6777 | actual = ds.transpose("x", "y") |
| 6778 | expected = ds.map(lambda x: x.transpose("x", "y", transpose_coords=True)) |
| 6779 | assert_identical(expected, actual) |
| 6780 | |
| 6781 | ds = create_test_data() |
| 6782 | actual = ds.transpose() |
| 6783 | for k in ds.variables: |
| 6784 | assert actual[k].dims[::-1] == ds[k].dims |
| 6785 | |
| 6786 | new_order = ("dim2", "dim3", "dim1", "time") |
| 6787 | actual = ds.transpose(*new_order) |
| 6788 | for k in ds.variables: |
| 6789 | expected_dims = tuple(d for d in new_order if d in ds[k].dims) |
| 6790 | assert actual[k].dims == expected_dims |
| 6791 | |
| 6792 | # same as above but with ellipsis |
| 6793 | new_order = ("dim2", "dim3", "dim1", "time") |
| 6794 | actual = ds.transpose("dim2", "dim3", ...) |
| 6795 | for k in ds.variables: |
| 6796 | expected_dims = tuple(d for d in new_order if d in ds[k].dims) |
| 6797 | assert actual[k].dims == expected_dims |
| 6798 | |
| 6799 | # test missing dimension, raise error |
| 6800 | with pytest.raises(ValueError): |
| 6801 | ds.transpose(..., "not_a_dim") |
| 6802 | |
| 6803 | # test missing dimension, ignore error |
| 6804 | actual = ds.transpose(..., "not_a_dim", missing_dims="ignore") |
| 6805 | expected_ell = ds.transpose(...) |
| 6806 | assert_identical(expected_ell, actual) |
nothing calls this directly
no test coverage detected