@@ -580,9 +580,17 @@ Transformation
580
580
--------------
581
581
582
582
The ``transform `` method returns an object that is indexed the same (same size)
583
- as the one being grouped. Thus, the passed transform function should return a
584
- result that is the same size as the group chunk. For example, suppose we wished
585
- to standardize the data within each group:
583
+ as the one being grouped. The transform function must:
584
+
585
+ * Return a result that is either the same size as the group chunk or
586
+ broadcastable to the size of the group chunk.
587
+ * Operate column-by-column on the group chunk. A fast path is used if the
588
+ transform function also operates on the entire group chunk as a DataFrame.
589
+ * Does not perform in-place operations on the group chunk. Group chunks should
590
+ be treated as immutable, and changes to a group chunk may produce unexpected
591
+ results.
592
+
593
+ For example, suppose we wished to standardize the data within each group:
586
594
587
595
.. ipython :: python
588
596
@@ -620,6 +628,14 @@ We can also visually compare the original and transformed data sets.
620
628
@savefig groupby_transform_plot.png
621
629
compare.plot()
622
630
631
+ Transformation functions that have lower dimension outputs are broadcast to
632
+ match the shape of the input array.
633
+
634
+ .. ipython :: python
635
+
636
+ extremum = lambda x : x.max() - x.min()
637
+ transformed = ts.groupby(key).transform(extremum)
638
+
623
639
Another common data transform is to replace missing data with the group mean.
624
640
625
641
.. ipython :: python
@@ -664,8 +680,9 @@ and that the transformed data contains no NAs.
664
680
665
681
.. note ::
666
682
667
- Some functions when applied to a groupby object will automatically transform the input, returning
668
- an object of the same shape as the original. Passing ``as_index=False `` will not affect these transformation methods.
683
+ Some functions when applied to a groupby object will automatically transform
684
+ the input, returning an object of the same shape as the original. Passing
685
+ ``as_index=False `` will not affect these transformation methods.
669
686
670
687
For example: ``fillna, ffill, bfill, shift ``.
671
688
0 commit comments