@@ -2684,8 +2684,9 @@ def test_weather_data_resample(use_flox):
2684
2684
assert expected .location .attrs == ds .location .attrs
2685
2685
2686
2686
2687
+ @pytest .mark .parametrize ("shuffle" , [True , False ])
2687
2688
@pytest .mark .parametrize ("use_flox" , [True , False ])
2688
- def test_multiple_groupers (use_flox ) -> None :
2689
+ def test_multiple_groupers (use_flox : bool , shuffle : bool ) -> None :
2689
2690
da = DataArray (
2690
2691
np .array ([1 , 2 , 3 , 0 , 2 , np .nan ]),
2691
2692
dims = "d" ,
@@ -2697,6 +2698,8 @@ def test_multiple_groupers(use_flox) -> None:
2697
2698
)
2698
2699
2699
2700
gb = da .groupby (labels1 = UniqueGrouper (), labels2 = UniqueGrouper ())
2701
+ if shuffle :
2702
+ gb = gb .shuffle ()
2700
2703
repr (gb )
2701
2704
2702
2705
expected = DataArray (
@@ -2716,6 +2719,8 @@ def test_multiple_groupers(use_flox) -> None:
2716
2719
coords = {"a" : ("x" , [0 , 0 , 1 , 1 ]), "b" : ("y" , [0 , 0 , 1 , 1 ])}
2717
2720
square = DataArray (np .arange (16 ).reshape (4 , 4 ), coords = coords , dims = ["x" , "y" ])
2718
2721
gb = square .groupby (a = UniqueGrouper (), b = UniqueGrouper ())
2722
+ if shuffle :
2723
+ gb = gb .shuffle ()
2719
2724
repr (gb )
2720
2725
with xr .set_options (use_flox = use_flox ):
2721
2726
actual = gb .mean ()
@@ -2739,11 +2744,15 @@ def test_multiple_groupers(use_flox) -> None:
2739
2744
dims = ["x" , "y" , "z" ],
2740
2745
)
2741
2746
gb = b .groupby (x = UniqueGrouper (), y = UniqueGrouper ())
2747
+ if shuffle :
2748
+ gb = gb .shuffle ()
2742
2749
repr (gb )
2743
2750
with xr .set_options (use_flox = use_flox ):
2744
2751
assert_identical (gb .mean ("z" ), b .mean ("z" ))
2745
2752
2746
2753
gb = b .groupby (x = UniqueGrouper (), xy = UniqueGrouper ())
2754
+ if shuffle :
2755
+ gb = gb .shuffle ()
2747
2756
repr (gb )
2748
2757
with xr .set_options (use_flox = use_flox ):
2749
2758
actual = gb .mean ()
@@ -2758,13 +2767,16 @@ def test_multiple_groupers(use_flox) -> None:
2758
2767
2759
2768
2760
2769
@pytest .mark .parametrize ("use_flox" , [True , False ])
2761
- def test_multiple_groupers_mixed (use_flox ) -> None :
2770
+ @pytest .mark .parametrize ("shuffle" , [True , False ])
2771
+ def test_multiple_groupers_mixed (use_flox : bool , shuffle : bool ) -> None :
2762
2772
# This groupby has missing groups
2763
2773
ds = xr .Dataset (
2764
2774
{"foo" : (("x" , "y" ), np .arange (12 ).reshape ((4 , 3 )))},
2765
2775
coords = {"x" : [10 , 20 , 30 , 40 ], "letters" : ("x" , list ("abba" ))},
2766
2776
)
2767
2777
gb = ds .groupby (x = BinGrouper (bins = [5 , 15 , 25 ]), letters = UniqueGrouper ())
2778
+ if shuffle :
2779
+ gb = gb .shuffle ()
2768
2780
expected_data = np .array (
2769
2781
[
2770
2782
[[0.0 , np .nan ], [np .nan , 3.0 ]],
@@ -2803,27 +2815,6 @@ def test_multiple_groupers_mixed(use_flox) -> None:
2803
2815
# ------
2804
2816
2805
2817
2806
- @requires_dask
2807
- def test_groupby_shuffle ():
2808
- import dask
2809
-
2810
- da = DataArray (
2811
- dask .array .from_array (np .array ([1 , 2 , 3 , 0 , 2 , np .nan ]), chunks = 2 ),
2812
- dims = "d" ,
2813
- coords = dict (
2814
- labels1 = ("d" , np .array (["a" , "b" , "c" , "c" , "b" , "a" ])),
2815
- labels2 = ("d" , np .array (["x" , "y" , "z" , "z" , "y" , "x" ])),
2816
- ),
2817
- name = "foo" ,
2818
- )
2819
-
2820
- gb = da .groupby ("labels1" )
2821
- shuffled = gb .shuffle ()
2822
- shuffled_obj = shuffled ._obj
2823
- with xr .set_options (use_flox = False ):
2824
- xr .testing .assert_identical (gb .mean (), shuffled .mean ())
2825
-
2826
-
2827
2818
# Possible property tests
2828
2819
# 1. lambda x: x
2829
2820
# 2. grouped-reduce on unique coords is identical to array
0 commit comments