@@ -112,6 +112,10 @@ def max_value(group):
112
112
def test_groupby_return_type ():
113
113
114
114
# GH2893, return a reduced type
115
+
116
+ def func (dataf ):
117
+ return dataf ["val2" ] - dataf ["val2" ].mean ()
118
+
115
119
df1 = DataFrame (
116
120
[
117
121
{"val1" : 1 , "val2" : 20 },
@@ -121,9 +125,6 @@ def test_groupby_return_type():
121
125
]
122
126
)
123
127
124
- def func (dataf ):
125
- return dataf ["val2" ] - dataf ["val2" ].mean ()
126
-
127
128
with tm .assert_produces_warning (FutureWarning ):
128
129
result = df1 .groupby ("val1" , squeeze = True ).apply (func )
129
130
assert isinstance (result , Series )
@@ -137,9 +138,6 @@ def func(dataf):
137
138
]
138
139
)
139
140
140
- def func (dataf ):
141
- return dataf ["val2" ] - dataf ["val2" ].mean ()
142
-
143
141
with tm .assert_produces_warning (FutureWarning ):
144
142
result = df2 .groupby ("val1" , squeeze = True ).apply (func )
145
143
assert isinstance (result , Series )
@@ -162,51 +160,51 @@ def test_inconsistent_return_type():
162
160
}
163
161
)
164
162
165
- def f (grp ):
163
+ def f_0 (grp ):
166
164
return grp .iloc [0 ]
167
165
168
166
expected = df .groupby ("A" ).first ()[["B" ]]
169
- result = df .groupby ("A" ).apply (f )[["B" ]]
167
+ result = df .groupby ("A" ).apply (f_0 )[["B" ]]
170
168
tm .assert_frame_equal (result , expected )
171
169
172
- def f (grp ):
170
+ def f_1 (grp ):
173
171
if grp .name == "Tiger" :
174
172
return None
175
173
return grp .iloc [0 ]
176
174
177
- result = df .groupby ("A" ).apply (f )[["B" ]]
175
+ result = df .groupby ("A" ).apply (f_1 )[["B" ]]
178
176
e = expected .copy ()
179
177
e .loc ["Tiger" ] = np .nan
180
178
tm .assert_frame_equal (result , e )
181
179
182
- def f (grp ):
180
+ def f_2 (grp ):
183
181
if grp .name == "Pony" :
184
182
return None
185
183
return grp .iloc [0 ]
186
184
187
- result = df .groupby ("A" ).apply (f )[["B" ]]
185
+ result = df .groupby ("A" ).apply (f_2 )[["B" ]]
188
186
e = expected .copy ()
189
187
e .loc ["Pony" ] = np .nan
190
188
tm .assert_frame_equal (result , e )
191
189
192
190
# 5592 revisited, with datetimes
193
- def f (grp ):
191
+ def f_3 (grp ):
194
192
if grp .name == "Pony" :
195
193
return None
196
194
return grp .iloc [0 ]
197
195
198
- result = df .groupby ("A" ).apply (f )[["C" ]]
196
+ result = df .groupby ("A" ).apply (f_3 )[["C" ]]
199
197
e = df .groupby ("A" ).first ()[["C" ]]
200
198
e .loc ["Pony" ] = pd .NaT
201
199
tm .assert_frame_equal (result , e )
202
200
203
201
# scalar outputs
204
- def f (grp ):
202
+ def f_4 (grp ):
205
203
if grp .name == "Pony" :
206
204
return None
207
205
return grp .iloc [0 ].loc ["C" ]
208
206
209
- result = df .groupby ("A" ).apply (f )
207
+ result = df .groupby ("A" ).apply (f_4 )
210
208
e = df .groupby ("A" ).first ()["C" ].copy ()
211
209
e .loc ["Pony" ] = np .nan
212
210
e .name = None
0 commit comments