| 5299 | ], |
| 5300 | ) |
| 5301 | def test_idxmin( |
| 5302 | self, |
| 5303 | x: np.ndarray, |
| 5304 | minindex: int | float, |
| 5305 | maxindex: int | float, |
| 5306 | nanindex: int | None, |
| 5307 | use_dask: bool, |
| 5308 | ) -> None: |
| 5309 | ar0_raw = xr.DataArray( |
| 5310 | x, dims=["x"], coords={"x": np.arange(x.size) * 4}, attrs=self.attrs |
| 5311 | ) |
| 5312 | if use_dask: |
| 5313 | ar0 = ar0_raw.chunk() |
| 5314 | else: |
| 5315 | ar0 = ar0_raw |
| 5316 | |
| 5317 | with pytest.raises( |
| 5318 | KeyError, |
| 5319 | match=r"'spam' not found in array dimensions", |
| 5320 | ): |
| 5321 | ar0.idxmin(dim="spam") |
| 5322 | |
| 5323 | # Scalar Dataarray |
| 5324 | with pytest.raises(ValueError): |
| 5325 | xr.DataArray(5).idxmin() |
| 5326 | |
| 5327 | coordarr0 = xr.DataArray(ar0.coords["x"].data, dims=["x"]) |
| 5328 | coordarr1 = coordarr0.copy() |
| 5329 | |
| 5330 | hasna = np.isnan(minindex) |
| 5331 | if np.isnan(minindex): |
| 5332 | minindex = 0 |
| 5333 | |
| 5334 | if hasna: |
| 5335 | coordarr1[...] = 1 |
| 5336 | fill_value_0 = np.nan |
| 5337 | else: |
| 5338 | fill_value_0 = 1 |
| 5339 | |
| 5340 | expected0 = ( |
| 5341 | (coordarr1 * fill_value_0).isel(x=minindex, drop=True).astype("float") |
| 5342 | ) |
| 5343 | expected0.name = "x" |
| 5344 | expected0.attrs = self.attrs # Default keeps attrs for reduction operations |
| 5345 | |
| 5346 | # Default fill value (NaN) |
| 5347 | result0 = ar0.idxmin() |
| 5348 | assert_identical(result0, expected0) |
| 5349 | |
| 5350 | # Manually specify NaN fill_value |
| 5351 | result1 = ar0.idxmin(fill_value=np.nan) |
| 5352 | assert_identical(result1, expected0) |
| 5353 | |
| 5354 | # keep_attrs |
| 5355 | result2 = ar0.idxmin(keep_attrs=True) |
| 5356 | expected2 = expected0.copy() |
| 5357 | expected2.attrs = self.attrs |
| 5358 | assert_identical(result2, expected2) |