|
| 1 | +"""Test for issue #10875: Clear error message when reducing over non-existent dimension.""" |
| 2 | +import numpy as np |
| 3 | +import pytest |
| 4 | + |
| 5 | +import xarray as xr |
| 6 | + |
| 7 | + |
| 8 | +class TestGroupbyDimensionError: |
| 9 | + """Tests for clearer error messages in groupby reduce operations.""" |
| 10 | + |
| 11 | + def test_groupby_reduce_missing_dim_single(self): |
| 12 | + """Groupby reduce with single missing dimension should have clear error.""" |
| 13 | + ds = xr.DataArray( |
| 14 | + np.reshape(range(27), (3, 3, 3)), |
| 15 | + coords=dict( |
| 16 | + lon=range(3), |
| 17 | + lat=range(3), |
| 18 | + time=xr.date_range("2025-10-01 00:00", "2025-10-01 02:00", freq="h"), |
| 19 | + ), |
| 20 | + ) |
| 21 | + |
| 22 | + with pytest.raises(ValueError, match=r"'longitude' not found in array dimensions"): |
| 23 | + ds.groupby("time").std(dim="longitude") |
| 24 | + |
| 25 | + def test_groupby_reduce_missing_dim_multiple(self): |
| 26 | + """Groupby reduce with multiple missing dimensions should list them.""" |
| 27 | + ds = xr.DataArray( |
| 28 | + np.reshape(range(27), (3, 3, 3)), |
| 29 | + coords=dict( |
| 30 | + lon=range(3), |
| 31 | + lat=range(3), |
| 32 | + time=xr.date_range("2025-10-01 00:00", "2025-10-01 02:00", freq="h"), |
| 33 | + ), |
| 34 | + ) |
| 35 | + |
| 36 | + with pytest.raises(ValueError, match=r"not found in array dimensions"): |
| 37 | + ds.groupby("time").std(dim=["longitude", "latitude"]) |
| 38 | + |
| 39 | + def test_standard_reduce_error_matches(self): |
| 40 | + """Standard reduce and groupby reduce should have similar error format.""" |
| 41 | + ds = xr.DataArray( |
| 42 | + np.reshape(range(27), (3, 3, 3)), |
| 43 | + coords=dict( |
| 44 | + lon=range(3), |
| 45 | + lat=range(3), |
| 46 | + time=xr.date_range("2025-10-01 00:00", "2025-10-01 02:00", freq="h"), |
| 47 | + ), |
| 48 | + ) |
| 49 | + |
| 50 | + standard_error_msg = None |
| 51 | + try: |
| 52 | + ds.std(dim="longitude") |
| 53 | + except ValueError as e: |
| 54 | + standard_error_msg = str(e) |
| 55 | + |
| 56 | + groupby_error_msg = None |
| 57 | + try: |
| 58 | + ds.groupby("time").std(dim="longitude") |
| 59 | + except ValueError as e: |
| 60 | + groupby_error_msg = str(e) |
| 61 | + |
| 62 | + assert "longitude" in standard_error_msg |
| 63 | + assert "longitude" in groupby_error_msg |
| 64 | + assert "not found in array dimensions" in standard_error_msg |
| 65 | + assert "not found in array dimensions" in groupby_error_msg |
| 66 | + |
| 67 | + def test_groupby_reduce_valid_dim_still_works(self): |
| 68 | + """Ensure valid dimensions still work correctly.""" |
| 69 | + ds = xr.DataArray( |
| 70 | + np.reshape(range(27), (3, 3, 3)), |
| 71 | + dims=["lon", "lat", "time"], |
| 72 | + coords=dict( |
| 73 | + lon=range(3), |
| 74 | + lat=range(3), |
| 75 | + time=xr.date_range("2025-10-01 00:00", "2025-10-01 02:00", freq="h"), |
| 76 | + ), |
| 77 | + ) |
| 78 | + |
| 79 | + result = ds.groupby("time").std(dim="lon") |
| 80 | + assert result is not None |
| 81 | + assert "lon" not in result.dims |
0 commit comments