@@ -1133,3 +1133,40 @@ def func(grp):
1133
1133
1134
1134
expected = pd .DataFrame ([2 , - 2 , 2 , 4 ], columns = ["B" ])
1135
1135
tm .assert_frame_equal (result , expected )
1136
+
1137
+
1138
+ def test_transform_lambda_indexing ():
1139
+ # GH 7883
1140
+ df = pd .DataFrame (
1141
+ {
1142
+ "A" : ["foo" , "bar" , "foo" , "bar" , "foo" , "flux" , "foo" , "flux" ],
1143
+ "B" : ["one" , "one" , "two" , "three" , "two" , "six" , "five" , "three" ],
1144
+ "C" : range (8 ),
1145
+ "D" : range (8 ),
1146
+ "E" : range (8 ),
1147
+ }
1148
+ )
1149
+ df = df .set_index (["A" , "B" ])
1150
+ df = df .sort_index ()
1151
+ result = df .groupby (level = "A" ).transform (lambda x : x .iloc [- 1 ])
1152
+ expected = DataFrame (
1153
+ {
1154
+ "C" : [3 , 3 , 7 , 7 , 4 , 4 , 4 , 4 ],
1155
+ "D" : [3 , 3 , 7 , 7 , 4 , 4 , 4 , 4 ],
1156
+ "E" : [3 , 3 , 7 , 7 , 4 , 4 , 4 , 4 ],
1157
+ },
1158
+ index = MultiIndex .from_tuples (
1159
+ [
1160
+ ("bar" , "one" ),
1161
+ ("bar" , "three" ),
1162
+ ("flux" , "six" ),
1163
+ ("flux" , "three" ),
1164
+ ("foo" , "five" ),
1165
+ ("foo" , "one" ),
1166
+ ("foo" , "two" ),
1167
+ ("foo" , "two" ),
1168
+ ],
1169
+ names = ["A" , "B" ],
1170
+ ),
1171
+ )
1172
+ tm .assert_frame_equal (result , expected )
0 commit comments