@@ -690,42 +690,74 @@ def test_linear_expression_groupby_with_name(v, use_fallback):
690
690
assert grouped .nterm == 10
691
691
692
692
693
- def test_linear_expression_groupby_with_series (v ):
693
+ @pytest .mark .parametrize ("use_fallback" , [True , False ])
694
+ def test_linear_expression_groupby_with_series (v , use_fallback ):
694
695
expr = 1 * v
695
696
groups = pd .Series ([1 ] * 10 + [2 ] * 10 , index = v .indexes ["dim_2" ])
696
- grouped = expr .groupby (groups ).sum ()
697
+ grouped = expr .groupby (groups ).sum (use_fallback = use_fallback )
697
698
assert "group" in grouped .dims
698
699
assert (grouped .data .group == [1 , 2 ]).all ()
699
700
assert grouped .nterm == 10
700
701
701
702
702
- def test_linear_expression_groupby_with_series_false (v ):
703
+ @pytest .mark .parametrize ("use_fallback" , [True , False ])
704
+ def test_linear_expression_groupby_series_with_name (v , use_fallback ):
705
+ expr = 1 * v
706
+ groups = pd .Series ([1 ] * 10 + [2 ] * 10 , index = v .indexes [v .dims [0 ]], name = "my_group" )
707
+ grouped = expr .groupby (groups ).sum (use_fallback = use_fallback )
708
+ assert "my_group" in grouped .dims
709
+ assert (grouped .data .my_group == [1 , 2 ]).all ()
710
+ assert grouped .nterm == 10
711
+
712
+
713
+ @pytest .mark .parametrize ("use_fallback" , [True , False ])
714
+ def test_linear_expression_groupby_with_series_false (v , use_fallback ):
703
715
expr = 1 * v
704
716
groups = pd .Series ([1 ] * 10 + [2 ] * 10 , index = v .indexes ["dim_2" ])
705
717
groups .name = "dim_2"
706
- with pytest .raises (ValueError ):
707
- expr .groupby (groups ).sum ()
718
+ if not use_fallback :
719
+ with pytest .raises (ValueError ):
720
+ expr .groupby (groups ).sum (use_fallback = use_fallback )
721
+ return
722
+ grouped = expr .groupby (groups ).sum (use_fallback = use_fallback )
723
+ assert "dim_2" in grouped .dims
724
+ assert (grouped .data .dim_2 == [1 , 2 ]).all ()
725
+ assert grouped .nterm == 10
708
726
709
727
710
- def test_linear_expression_groupby_with_dataframe (v ):
728
+ @pytest .mark .parametrize ("use_fallback" , [True , False ])
729
+ def test_linear_expression_groupby_with_dataframe (v , use_fallback ):
711
730
expr = 1 * v
712
731
groups = pd .DataFrame (
713
732
{"a" : [1 ] * 10 + [2 ] * 10 , "b" : list (range (4 )) * 5 }, index = v .indexes ["dim_2" ]
714
733
)
715
- grouped = expr .groupby (groups ).sum ()
734
+ if use_fallback :
735
+ with pytest .raises (ValueError ):
736
+ expr .groupby (groups ).sum (use_fallback = use_fallback )
737
+ return
738
+
739
+ grouped = expr .groupby (groups ).sum (use_fallback = use_fallback )
716
740
index = pd .MultiIndex .from_frame (groups )
717
741
assert "group" in grouped .dims
718
742
assert set (grouped .data .group .values ) == set (index .values )
719
743
assert grouped .nterm == 3
720
744
721
745
722
- def test_linear_expression_groupby_with_dataarray (v ):
746
+ @pytest .mark .parametrize ("use_fallback" , [True , False ])
747
+ def test_linear_expression_groupby_with_dataarray (v , use_fallback ):
723
748
expr = 1 * v
724
749
df = pd .DataFrame (
725
750
{"a" : [1 ] * 10 + [2 ] * 10 , "b" : list (range (4 )) * 5 }, index = v .indexes ["dim_2" ]
726
751
)
727
752
groups = xr .DataArray (df )
728
- grouped = expr .groupby (groups ).sum ()
753
+
754
+ # this should not be the case, see https://github.com/PyPSA/linopy/issues/351
755
+ if use_fallback :
756
+ with pytest .raises (KeyError ):
757
+ expr .groupby (groups ).sum (use_fallback = use_fallback )
758
+ return
759
+
760
+ grouped = expr .groupby (groups ).sum (use_fallback = use_fallback )
729
761
index = pd .MultiIndex .from_frame (df )
730
762
assert "group" in grouped .dims
731
763
assert set (grouped .data .group .values ) == set (index .values )
0 commit comments