@@ -2111,3 +2111,26 @@ def test_subsetting_columns_keeps_attrs(klass, attr, value):
2111
2111
expected = df .groupby ("a" , ** {attr : value })
2112
2112
result = expected [["b" ]] if klass is DataFrame else expected ["b" ]
2113
2113
assert getattr (result , attr ) == getattr (expected , attr )
2114
+
2115
+
2116
+ @pytest .mark .parametrize ("func" , ["sum" , "any" , "shift" ])
2117
+ def test_groupby_column_index_name_lost (func ):
2118
+ # GH: 29764 groupby loses index sometimes
2119
+ expected = pd .Index (["a" ], name = "idx" )
2120
+ df = pd .DataFrame ([[1 ]], columns = expected )
2121
+ df_grouped = df .groupby ([1 ])
2122
+ result = getattr (df_grouped , func )().columns
2123
+ tm .assert_index_equal (result , expected )
2124
+
2125
+
2126
+ @pytest .mark .parametrize ("func" , ["ffill" , "bfill" ])
2127
+ def test_groupby_column_index_name_lost_fill_funcs (func ):
2128
+ # GH: 29764 groupby loses index sometimes
2129
+ df = pd .DataFrame (
2130
+ [[1 , 1.0 , - 1.0 ], [1 , np .nan , np .nan ], [1 , 2.0 , - 2.0 ]],
2131
+ columns = pd .Index (["type" , "a" , "b" ], name = "idx" ),
2132
+ )
2133
+ df_grouped = df .groupby (["type" ])[["a" , "b" ]]
2134
+ result = getattr (df_grouped , func )().columns
2135
+ expected = pd .Index (["a" , "b" ], name = "idx" )
2136
+ tm .assert_index_equal (result , expected )
0 commit comments