@@ -160,14 +160,19 @@ class providing the base-class of operations.
160
160
side-effects, as they will take effect twice for the first
161
161
group.
162
162
163
+ .. versionchanged:: 1.3.0
164
+
165
+ The resulting dtype will reflect the return value of the passed ``func``,
166
+ see the examples below.
167
+
163
168
Examples
164
169
--------
165
170
{examples}
166
171
""" ,
167
172
"dataframe_examples" : """
168
173
>>> df = pd.DataFrame({'A': 'a a b'.split(),
169
174
... 'B': [1,2,3],
170
- ... 'C': [4,6, 5]})
175
+ ... 'C': [4,6,5]})
171
176
>>> g = df.groupby('A')
172
177
173
178
Notice that ``g`` has two groups, ``a`` and ``b``.
@@ -185,13 +190,17 @@ class providing the base-class of operations.
185
190
186
191
Example 2: The function passed to `apply` takes a DataFrame as
187
192
its argument and returns a Series. `apply` combines the result for
188
- each group together into a new DataFrame:
193
+ each group together into a new DataFrame.
194
+
195
+ .. versionchanged:: 1.3.0
189
196
190
- >>> g[['B', 'C']].apply(lambda x: x.max() - x.min())
191
- B C
197
+ The resulting dtype will reflect the return value of the passed ``func``.
198
+
199
+ >>> g[['B', 'C']].apply(lambda x: x.astype(float).max() - x.min())
200
+ B C
192
201
A
193
- a 1 2
194
- b 0 0
202
+ a 1.0 2.0
203
+ b 0.0 0. 0
195
204
196
205
Example 3: The function passed to `apply` takes a DataFrame as
197
206
its argument and returns a scalar. `apply` combines the result for
@@ -212,12 +221,16 @@ class providing the base-class of operations.
212
221
213
222
Example 1: The function passed to `apply` takes a Series as
214
223
its argument and returns a Series. `apply` combines the result for
215
- each group together into a new Series:
224
+ each group together into a new Series.
225
+
226
+ .. versionchanged:: 1.3.0
216
227
217
- >>> g.apply(lambda x: x*2 if x.name == 'b' else x/2)
228
+ The resulting dtype will reflect the return value of the passed ``func``.
229
+
230
+ >>> g.apply(lambda x: x*2 if x.name == 'a' else x/2)
218
231
a 0.0
219
- a 0.5
220
- b 4 .0
232
+ a 2.0
233
+ b 1 .0
221
234
dtype: float64
222
235
223
236
Example 2: The function passed to `apply` takes a Series as
@@ -369,12 +382,17 @@ class providing the base-class of operations.
369
382
in the subframe. If f also supports application to the entire subframe,
370
383
then a fast path is used starting from the second chunk.
371
384
* f must not mutate groups. Mutation is not supported and may
372
- produce unexpected results. See :ref:`udf-mutation` for more details.
385
+ produce unexpected results. See :ref:`gotchas. udf-mutation` for more details.
373
386
374
387
When using ``engine='numba'``, there will be no "fall back" behavior internally.
375
388
The group data and group index will be passed as numpy arrays to the JITed
376
389
user defined function, and no alternative execution attempts will be tried.
377
390
391
+ .. versionchanged:: 1.3.0
392
+
393
+ The resulting dtype will reflect the return value of the passed ``func``,
394
+ see the examples below.
395
+
378
396
Examples
379
397
--------
380
398
@@ -404,6 +422,20 @@ class providing the base-class of operations.
404
422
3 3 8.0
405
423
4 4 6.0
406
424
5 3 8.0
425
+
426
+ .. versionchanged:: 1.3.0
427
+
428
+ The resulting dtype will reflect the return value of the passed ``func``,
429
+ for example:
430
+
431
+ >>> grouped[['C', 'D']].transform(lambda x: x.astype(int).max())
432
+ C D
433
+ 0 5 8
434
+ 1 5 9
435
+ 2 5 8
436
+ 3 5 9
437
+ 4 5 8
438
+ 5 5 9
407
439
"""
408
440
409
441
_agg_template = """
@@ -471,12 +503,16 @@ class providing the base-class of operations.
471
503
When using ``engine='numba'``, there will be no "fall back" behavior internally.
472
504
The group data and group index will be passed as numpy arrays to the JITed
473
505
user defined function, and no alternative execution attempts will be tried.
474
- {examples}
475
506
476
507
Functions that mutate the passed object can produce unexpected
477
- behavior or errors and are not supported. See :ref:`udf-mutation`
508
+ behavior or errors and are not supported. See :ref:`gotchas. udf-mutation`
478
509
for more details.
479
- """
510
+
511
+ .. versionchanged:: 1.3.0
512
+
513
+ The resulting dtype will reflect the return value of the passed ``func``,
514
+ see the examples below.
515
+ {examples}"""
480
516
481
517
482
518
@final
@@ -1237,9 +1273,6 @@ def _python_agg_general(self, func, *args, **kwargs):
1237
1273
assert result is not None
1238
1274
key = base .OutputKey (label = name , position = idx )
1239
1275
1240
- if is_numeric_dtype (obj .dtype ):
1241
- result = maybe_downcast_numeric (result , obj .dtype )
1242
-
1243
1276
if self .grouper ._filter_empty_groups :
1244
1277
mask = counts .ravel () > 0
1245
1278
0 commit comments