@@ -83,6 +83,8 @@ def test_rolling(self, f):
83
83
84
84
result = getattr (r , f )()
85
85
expected = g .apply (lambda x : getattr (x .rolling (4 ), f )())
86
+ # groupby.apply doesn't drop the grouped-by column
87
+ expected = expected .drop ("A" , axis = 1 )
86
88
# GH 39732
87
89
expected_index = MultiIndex .from_arrays ([self .frame ["A" ], range (40 )])
88
90
expected .index = expected_index
@@ -95,6 +97,8 @@ def test_rolling_ddof(self, f):
95
97
96
98
result = getattr (r , f )(ddof = 1 )
97
99
expected = g .apply (lambda x : getattr (x .rolling (4 ), f )(ddof = 1 ))
100
+ # groupby.apply doesn't drop the grouped-by column
101
+ expected = expected .drop ("A" , axis = 1 )
98
102
# GH 39732
99
103
expected_index = MultiIndex .from_arrays ([self .frame ["A" ], range (40 )])
100
104
expected .index = expected_index
@@ -111,6 +115,8 @@ def test_rolling_quantile(self, interpolation):
111
115
expected = g .apply (
112
116
lambda x : x .rolling (4 ).quantile (0.4 , interpolation = interpolation )
113
117
)
118
+ # groupby.apply doesn't drop the grouped-by column
119
+ expected = expected .drop ("A" , axis = 1 )
114
120
# GH 39732
115
121
expected_index = MultiIndex .from_arrays ([self .frame ["A" ], range (40 )])
116
122
expected .index = expected_index
@@ -147,6 +153,8 @@ def test_rolling_apply(self, raw):
147
153
# reduction
148
154
result = r .apply (lambda x : x .sum (), raw = raw )
149
155
expected = g .apply (lambda x : x .rolling (4 ).apply (lambda y : y .sum (), raw = raw ))
156
+ # groupby.apply doesn't drop the grouped-by column
157
+ expected = expected .drop ("A" , axis = 1 )
150
158
# GH 39732
151
159
expected_index = MultiIndex .from_arrays ([self .frame ["A" ], range (40 )])
152
160
expected .index = expected_index
@@ -442,6 +450,8 @@ def test_groupby_rolling_empty_frame(self):
442
450
# GH 36197
443
451
expected = DataFrame ({"s1" : []})
444
452
result = expected .groupby ("s1" ).rolling (window = 1 ).sum ()
453
+ # GH 32262
454
+ expected = expected .drop (columns = "s1" )
445
455
# GH-38057 from_tuples gives empty object dtype, we now get float/int levels
446
456
# expected.index = MultiIndex.from_tuples([], names=["s1", None])
447
457
expected .index = MultiIndex .from_product (
@@ -451,6 +461,8 @@ def test_groupby_rolling_empty_frame(self):
451
461
452
462
expected = DataFrame ({"s1" : [], "s2" : []})
453
463
result = expected .groupby (["s1" , "s2" ]).rolling (window = 1 ).sum ()
464
+ # GH 32262
465
+ expected = expected .drop (columns = ["s1" , "s2" ])
454
466
expected .index = MultiIndex .from_product (
455
467
[
456
468
Index ([], dtype = "float64" ),
@@ -503,6 +515,8 @@ def test_groupby_rolling_no_sort(self):
503
515
columns = ["foo" , "bar" ],
504
516
index = MultiIndex .from_tuples ([(2 , 0 ), (1 , 1 )], names = ["foo" , None ]),
505
517
)
518
+ # GH 32262
519
+ expected = expected .drop (columns = "foo" )
506
520
tm .assert_frame_equal (result , expected )
507
521
508
522
def test_groupby_rolling_count_closed_on (self ):
@@ -553,6 +567,8 @@ def test_groupby_rolling_sem(self, func, kwargs):
553
567
[("a" , 0 ), ("a" , 1 ), ("b" , 2 ), ("b" , 3 ), ("b" , 4 )], names = ["a" , None ]
554
568
),
555
569
)
570
+ # GH 32262
571
+ expected = expected .drop (columns = "a" )
556
572
tm .assert_frame_equal (result , expected )
557
573
558
574
@pytest .mark .parametrize (
@@ -666,6 +682,19 @@ def test_groupby_rolling_object_doesnt_affect_groupby_apply(self):
666
682
assert not g .mutated
667
683
assert not g .grouper .mutated
668
684
685
+ @pytest .mark .parametrize (
686
+ "columns" , [MultiIndex .from_tuples ([("A" , "" ), ("B" , "C" )]), ["A" , "B" ]]
687
+ )
688
+ def test_by_column_not_in_values (self , columns ):
689
+ # GH 32262
690
+ df = DataFrame ([[1 , 0 ]] * 20 + [[2 , 0 ]] * 12 + [[3 , 0 ]] * 8 , columns = columns )
691
+ g = df .groupby ("A" )
692
+ original_obj = g .obj .copy (deep = True )
693
+ r = g .rolling (4 )
694
+ result = r .sum ()
695
+ assert "A" not in result .columns
696
+ tm .assert_frame_equal (g .obj , original_obj )
697
+
669
698
670
699
class TestExpanding :
671
700
def setup_method (self ):
@@ -680,6 +709,8 @@ def test_expanding(self, f):
680
709
681
710
result = getattr (r , f )()
682
711
expected = g .apply (lambda x : getattr (x .expanding (), f )())
712
+ # groupby.apply doesn't drop the grouped-by column
713
+ expected = expected .drop ("A" , axis = 1 )
683
714
# GH 39732
684
715
expected_index = MultiIndex .from_arrays ([self .frame ["A" ], range (40 )])
685
716
expected .index = expected_index
@@ -692,6 +723,8 @@ def test_expanding_ddof(self, f):
692
723
693
724
result = getattr (r , f )(ddof = 0 )
694
725
expected = g .apply (lambda x : getattr (x .expanding (), f )(ddof = 0 ))
726
+ # groupby.apply doesn't drop the grouped-by column
727
+ expected = expected .drop ("A" , axis = 1 )
695
728
# GH 39732
696
729
expected_index = MultiIndex .from_arrays ([self .frame ["A" ], range (40 )])
697
730
expected .index = expected_index
@@ -708,6 +741,8 @@ def test_expanding_quantile(self, interpolation):
708
741
expected = g .apply (
709
742
lambda x : x .expanding ().quantile (0.4 , interpolation = interpolation )
710
743
)
744
+ # groupby.apply doesn't drop the grouped-by column
745
+ expected = expected .drop ("A" , axis = 1 )
711
746
# GH 39732
712
747
expected_index = MultiIndex .from_arrays ([self .frame ["A" ], range (40 )])
713
748
expected .index = expected_index
@@ -748,6 +783,8 @@ def test_expanding_apply(self, raw):
748
783
# reduction
749
784
result = r .apply (lambda x : x .sum (), raw = raw )
750
785
expected = g .apply (lambda x : x .expanding ().apply (lambda y : y .sum (), raw = raw ))
786
+ # groupby.apply doesn't drop the grouped-by column
787
+ expected = expected .drop ("A" , axis = 1 )
751
788
# GH 39732
752
789
expected_index = MultiIndex .from_arrays ([self .frame ["A" ], range (40 )])
753
790
expected .index = expected_index
0 commit comments