| 5786 | assert_identical(result3, expected2) |
| 5787 | |
| 5788 | def test_argmin( |
| 5789 | self, |
| 5790 | x: np.ndarray, |
| 5791 | minindex: list[int | float], |
| 5792 | maxindex: list[int | float], |
| 5793 | nanindex: list[int | None], |
| 5794 | ) -> None: |
| 5795 | ar = xr.DataArray( |
| 5796 | x, |
| 5797 | dims=["y", "x"], |
| 5798 | coords={"x": np.arange(x.shape[1]) * 4, "y": 1 - np.arange(x.shape[0])}, |
| 5799 | attrs=self.attrs, |
| 5800 | ) |
| 5801 | indarrnp = np.tile(np.arange(x.shape[1], dtype=np.intp), [x.shape[0], 1]) |
| 5802 | indarr = xr.DataArray(indarrnp, dims=ar.dims, coords=ar.coords) |
| 5803 | |
| 5804 | if np.isnan(minindex).any(): |
| 5805 | with pytest.raises(ValueError): |
| 5806 | ar.argmin(dim="x") |
| 5807 | return |
| 5808 | |
| 5809 | expected0list = [ |
| 5810 | indarr.isel(y=yi).isel(x=indi, drop=True) |
| 5811 | for yi, indi in enumerate(minindex) |
| 5812 | ] |
| 5813 | expected0 = xr.concat(expected0list, dim="y") |
| 5814 | expected0.attrs = self.attrs # Default keeps attrs for reduction operations |
| 5815 | |
| 5816 | result0 = ar.argmin(dim="x") |
| 5817 | assert_identical(result0, expected0) |
| 5818 | |
| 5819 | result1 = ar.argmin(axis=1) |
| 5820 | assert_identical(result1, expected0) |
| 5821 | |
| 5822 | result2 = ar.argmin(dim="x", keep_attrs=True) |
| 5823 | expected1 = expected0.copy() |
| 5824 | expected1.attrs = self.attrs |
| 5825 | assert_identical(result2, expected1) |
| 5826 | |
| 5827 | minindex = [ |
| 5828 | x if y is None or ar.dtype.kind == "O" else y |
| 5829 | for x, y in zip(minindex, nanindex, strict=True) |
| 5830 | ] |
| 5831 | expected2list = [ |
| 5832 | indarr.isel(y=yi).isel(x=indi, drop=True) |
| 5833 | for yi, indi in enumerate(minindex) |
| 5834 | ] |
| 5835 | expected2 = xr.concat(expected2list, dim="y") |
| 5836 | expected2.attrs = self.attrs # Default keeps attrs for reduction operations |
| 5837 | |
| 5838 | result3 = ar.argmin(dim="x", skipna=False) |
| 5839 | |
| 5840 | assert_identical(result3, expected2) |
| 5841 | |
| 5842 | def test_argmax( |
| 5843 | self, |