@@ -1302,8 +1302,15 @@ def test_groupby_math_not_aligned(self):
1302
1302
expected = DataArray ([10 , 11 , np .nan , np .nan ], array .coords )
1303
1303
assert_identical (expected , actual )
1304
1304
1305
+ # regression test for #7797
1306
+ other = array .groupby ("b" ).sum ()
1307
+ actual = array .sel (x = [0 , 1 ]).groupby ("b" ) - other
1308
+ expected = DataArray ([- 1 , 0 ], {"b" : ("x" , [0 , 0 ]), "x" : [0 , 1 ]}, dims = "x" )
1309
+ assert_identical (expected , actual )
1310
+
1305
1311
other = DataArray ([10 ], coords = {"c" : 123 , "b" : [0 ]}, dims = "b" )
1306
1312
actual = array .groupby ("b" ) + other
1313
+ expected = DataArray ([10 , 11 , np .nan , np .nan ], array .coords )
1307
1314
expected .coords ["c" ] = (["x" ], [123 ] * 2 + [np .nan ] * 2 )
1308
1315
assert_identical (expected , actual )
1309
1316
@@ -2289,3 +2296,20 @@ def test_resample_cumsum(method: str, expected_array: list[float]) -> None:
2289
2296
actual = getattr (ds .foo .resample (time = "3M" ), method )(dim = "time" )
2290
2297
expected .coords ["time" ] = ds .time
2291
2298
assert_identical (expected .drop_vars (["time" ]).foo , actual )
2299
+
2300
+
2301
+ def test_groupby_binary_op_regression ():
2302
+ # regression test for #7797
2303
+ # monthly timeseries that should return "zero anomalies" everywhere
2304
+ time = xr .date_range ("2023-01-01" , "2023-12-31" , freq = "MS" )
2305
+ data = np .linspace (- 1 , 1 , 12 )
2306
+ x = xr .DataArray (data , coords = {"time" : time })
2307
+ clim = xr .DataArray (data , coords = {"month" : np .arange (1 , 13 , 1 )})
2308
+
2309
+ # seems to give the correct result if we use the full x, but not with a slice
2310
+ x_slice = x .sel (time = ["2023-04-01" ])
2311
+
2312
+ # two typical ways of computing anomalies
2313
+ anom_gb = x_slice .groupby ("time.month" ) - clim
2314
+
2315
+ assert_identical (xr .zeros_like (anom_gb ), anom_gb )
0 commit comments