@@ -580,9 +580,21 @@ 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 (e.g., a scalar,
587
+ ``grouped.transform(lambda x: x.iloc[-1]) ``).
588
+ * Operate column-by-column on the group chunk. The transform is applied to
589
+ the first group chunk using chunk.apply.
590
+ * Not perform in-place operations on the group chunk. Group chunks should
591
+ be treated as immutable, and changes to a group chunk may produce unexpected
592
+ results. For example, when using ``fillna ``, ``inplace `` must be ``False ``
593
+ (``grouped.transform(lambda x: x.fillna(inplace=False)) ``).
594
+ * (Optionally) operates on the entire group chunk. If this is supported, a
595
+ fast path is used starting from the *second * chunk.
596
+
597
+ For example, suppose we wished to standardize the data within each group:
586
598
587
599
.. ipython :: python
588
600
@@ -620,6 +632,21 @@ We can also visually compare the original and transformed data sets.
620
632
@savefig groupby_transform_plot.png
621
633
compare.plot()
622
634
635
+ Transformation functions that have lower dimension outputs are broadcast to
636
+ match the shape of the input array.
637
+
638
+ .. ipython :: python
639
+
640
+ data_range = lambda x : x.max() - x.min()
641
+ ts.groupby(key).transform(data_range)
642
+
643
+ Alternatively the built-in methods can be could be used to produce the same
644
+ outputs
645
+
646
+ .. ipython :: python
647
+
648
+ ts.groupby(key).transform(' max' ) - ts.groupby(key).transform(' min' )
649
+
623
650
Another common data transform is to replace missing data with the group mean.
624
651
625
652
.. ipython :: python
@@ -664,8 +691,9 @@ and that the transformed data contains no NAs.
664
691
665
692
.. note ::
666
693
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.
694
+ Some functions when applied to a groupby object will automatically transform
695
+ the input, returning an object of the same shape as the original. Passing
696
+ ``as_index=False `` will not affect these transformation methods.
669
697
670
698
For example: ``fillna, ffill, bfill, shift ``.
671
699
0 commit comments