@@ -873,7 +873,14 @@ def test_groupby_dataset_errors() -> None:
873
873
data .groupby (data .coords ["dim1" ].to_index ())
874
874
875
875
876
- def test_groupby_dataset_reduce () -> None :
876
+ @pytest .mark .parametrize (
877
+ "by_func" ,
878
+ [
879
+ pytest .param (lambda x : x , id = "group-by-string" ),
880
+ pytest .param (lambda x : {x : UniqueGrouper ()}, id = "group-by-unique-grouper" ),
881
+ ],
882
+ )
883
+ def test_groupby_dataset_reduce_ellipsis (by_func ) -> None :
877
884
data = Dataset (
878
885
{
879
886
"xy" : (["x" , "y" ], np .random .randn (3 , 4 )),
@@ -885,12 +892,12 @@ def test_groupby_dataset_reduce() -> None:
885
892
886
893
expected = data .mean ("y" )
887
894
expected ["yonly" ] = expected ["yonly" ].variable .set_dims ({"x" : 3 })
888
- for gb in [ data .groupby ("x" ), data . groupby ( x = UniqueGrouper ())]:
889
- actual = gb .mean (...)
890
- assert_allclose (expected , actual )
895
+ gb = data .groupby (by_func ( "x" ))
896
+ actual = gb .mean (...)
897
+ assert_allclose (expected , actual )
891
898
892
- actual = gb .mean ("y" )
893
- assert_allclose (expected , actual )
899
+ actual = gb .mean ("y" )
900
+ assert_allclose (expected , actual )
894
901
895
902
letters = data ["letters" ]
896
903
expected = Dataset (
@@ -900,9 +907,9 @@ def test_groupby_dataset_reduce() -> None:
900
907
"yonly" : data ["yonly" ].groupby (letters ).mean (),
901
908
}
902
909
)
903
- for gb in [ data .groupby ("letters" ), data . groupby ( letters = UniqueGrouper ())]:
904
- actual = gb .mean (...)
905
- assert_allclose (expected , actual )
910
+ gb = data .groupby (by_func ( "letters" ))
911
+ actual = gb .mean (...)
912
+ assert_allclose (expected , actual )
906
913
907
914
908
915
@pytest .mark .parametrize ("squeeze" , [True , False ])
@@ -1040,23 +1047,25 @@ def test_groupby_bins_cut_kwargs(use_flox: bool) -> None:
1040
1047
1041
1048
1042
1049
@pytest .mark .parametrize ("indexed_coord" , [True , False ])
1043
- def test_groupby_bins_math (indexed_coord ) -> None :
1050
+ @pytest .mark .parametrize (
1051
+ ["groupby_method" , "args" ],
1052
+ (
1053
+ ("groupby_bins" , ("x" , np .arange (0 , 8 , 3 ))),
1054
+ ("groupby" , ({"x" : BinGrouper (bins = np .arange (0 , 8 , 3 ))},)),
1055
+ ),
1056
+ )
1057
+ def test_groupby_bins_math (groupby_method , args , indexed_coord ) -> None :
1044
1058
N = 7
1045
1059
da = DataArray (np .random .random ((N , N )), dims = ("x" , "y" ))
1046
1060
if indexed_coord :
1047
1061
da ["x" ] = np .arange (N )
1048
1062
da ["y" ] = np .arange (N )
1049
1063
1050
- for g in [
1051
- da .groupby_bins ("x" , np .arange (0 , N + 1 , 3 )),
1052
- da .groupby (x = BinGrouper (bins = np .arange (0 , N + 1 , 3 ))),
1053
- ]:
1054
- mean = g .mean ()
1055
- expected = da .isel (x = slice (1 , None )) - mean .isel (
1056
- x_bins = ("x" , [0 , 0 , 0 , 1 , 1 , 1 ])
1057
- )
1058
- actual = g - mean
1059
- assert_identical (expected , actual )
1064
+ g = getattr (da , groupby_method )(* args )
1065
+ mean = g .mean ()
1066
+ expected = da .isel (x = slice (1 , None )) - mean .isel (x_bins = ("x" , [0 , 0 , 0 , 1 , 1 , 1 ]))
1067
+ actual = g - mean
1068
+ assert_identical (expected , actual )
1060
1069
1061
1070
1062
1071
def test_groupby_math_nD_group () -> None :
0 commit comments