(self)
| 1749 | assert_equal(data.isel(td=slice(1, 3)), data.sel(td=slice("1 days", "2 days"))) |
| 1750 | |
| 1751 | def test_sel_dataarray(self) -> None: |
| 1752 | data = create_test_data() |
| 1753 | |
| 1754 | ind = DataArray([0.0, 0.5, 1.0], dims=["dim2"]) |
| 1755 | actual = data.sel(dim2=ind) |
| 1756 | assert_equal(actual, data.isel(dim2=[0, 1, 2])) |
| 1757 | |
| 1758 | # with different dimension |
| 1759 | ind = DataArray([0.0, 0.5, 1.0], dims=["new_dim"]) |
| 1760 | actual = data.sel(dim2=ind) |
| 1761 | expected = data.isel(dim2=Variable("new_dim", [0, 1, 2])) |
| 1762 | assert "new_dim" in actual.dims |
| 1763 | assert_equal(actual, expected) |
| 1764 | |
| 1765 | # Multi-dimensional |
| 1766 | ind = DataArray([[0.0], [0.5], [1.0]], dims=["new_dim", "new_dim2"]) |
| 1767 | actual = data.sel(dim2=ind) |
| 1768 | expected = data.isel(dim2=Variable(("new_dim", "new_dim2"), [[0], [1], [2]])) |
| 1769 | assert "new_dim" in actual.dims |
| 1770 | assert "new_dim2" in actual.dims |
| 1771 | assert_equal(actual, expected) |
| 1772 | |
| 1773 | # with coordinate |
| 1774 | ind = DataArray( |
| 1775 | [0.0, 0.5, 1.0], dims=["new_dim"], coords={"new_dim": ["a", "b", "c"]} |
| 1776 | ) |
| 1777 | actual = data.sel(dim2=ind) |
| 1778 | expected = data.isel(dim2=[0, 1, 2]).rename({"dim2": "new_dim"}) |
| 1779 | assert "new_dim" in actual.dims |
| 1780 | assert "new_dim" in actual.coords |
| 1781 | assert_equal( |
| 1782 | actual.drop_vars("new_dim").drop_vars("dim2"), expected.drop_vars("new_dim") |
| 1783 | ) |
| 1784 | assert_equal(actual["new_dim"].drop_vars("dim2"), ind["new_dim"]) |
| 1785 | |
| 1786 | # with conflicted coordinate (silently ignored) |
| 1787 | ind = DataArray( |
| 1788 | [0.0, 0.5, 1.0], dims=["dim2"], coords={"dim2": ["a", "b", "c"]} |
| 1789 | ) |
| 1790 | actual = data.sel(dim2=ind) |
| 1791 | expected = data.isel(dim2=[0, 1, 2]) |
| 1792 | assert_equal(actual, expected) |
| 1793 | |
| 1794 | # with conflicted coordinate (silently ignored) |
| 1795 | ind = DataArray( |
| 1796 | [0.0, 0.5, 1.0], |
| 1797 | dims=["new_dim"], |
| 1798 | coords={"new_dim": ["a", "b", "c"], "dim2": 3}, |
| 1799 | ) |
| 1800 | actual = data.sel(dim2=ind) |
| 1801 | assert_equal( |
| 1802 | actual["new_dim"].drop_vars("dim2"), ind["new_dim"].drop_vars("dim2") |
| 1803 | ) |
| 1804 | expected = data.isel(dim2=[0, 1, 2]) |
| 1805 | expected["dim2"] = (("new_dim"), expected["dim2"].values) |
| 1806 | assert_equal(actual["dim2"].drop_vars("new_dim"), expected["dim2"]) |
| 1807 | assert actual["var1"].dims == ("dim1", "new_dim") |
| 1808 |
nothing calls this directly
no test coverage detected