@@ -632,52 +632,185 @@ def _wrap_applied_output(
632632 result .index = default_index (len (result ))
633633 return result .__finalize__ (self .obj , method = "groupby" )
634634
635- __examples_series_doc = dedent (
635+ # __examples_series_doc = dedent(
636+ # """
637+ # >>> ser = pd.Series([390.0, 350.0, 30.0, 20.0],
638+ # ... index=["Falcon", "Falcon", "Parrot", "Parrot"],
639+ # ... name="Max Speed")
640+ # >>> grouped = ser.groupby([1, 1, 2, 2])
641+ # >>> grouped.transform(lambda x: (x - x.mean()) / x.std())
642+ # Falcon 0.707107
643+ # Falcon -0.707107
644+ # Parrot 0.707107
645+ # Parrot -0.707107
646+ # Name: Max Speed, dtype: float64
647+
648+ # Broadcast result of the transformation
649+
650+ # >>> grouped.transform(lambda x: x.max() - x.min())
651+ # Falcon 40.0
652+ # Falcon 40.0
653+ # Parrot 10.0
654+ # Parrot 10.0
655+ # Name: Max Speed, dtype: float64
656+
657+ # >>> grouped.transform("mean")
658+ # Falcon 370.0
659+ # Falcon 370.0
660+ # Parrot 25.0
661+ # Parrot 25.0
662+ # Name: Max Speed, dtype: float64
663+
664+ # .. versionchanged:: 1.3.0
665+
666+ # The resulting dtype will reflect the return value of the passed ``func``,
667+ # for example:
668+
669+ # >>> grouped.transform(lambda x: x.astype(int).max())
670+ # Falcon 390
671+ # Falcon 390
672+ # Parrot 30
673+ # Parrot 30
674+ # Name: Max Speed, dtype: int64
675+ # """
676+ # )
677+
678+ # @Substitution(klass="Series", example=__examples_series_doc)
679+ # @Appender(_transform_template)
680+ def transform (self , func , * args , engine = None , engine_kwargs = None , ** kwargs ):
636681 """
637- >>> ser = pd.Series([390.0, 350.0, 30.0, 20.0],
638- ... index=["Falcon", "Falcon", "Parrot", "Parrot"],
639- ... name="Max Speed")
640- >>> grouped = ser.groupby([1, 1, 2, 2])
641- >>> grouped.transform(lambda x: (x - x.mean()) / x.std())
642- Falcon 0.707107
643- Falcon -0.707107
644- Parrot 0.707107
645- Parrot -0.707107
646- Name: Max Speed, dtype: float64
682+ Test Call function producing a same-indexed Series on each group.
647683
648- Broadcast result of the transformation
684+ Returns a Series having the same indexes as the original object
685+ filled with the transformed values.
649686
650- >>> grouped.transform(lambda x: x.max() - x.min())
651- Falcon 40.0
652- Falcon 40.0
653- Parrot 10.0
654- Parrot 10.0
655- Name: Max Speed, dtype: float64
687+ Parameters
688+ ----------
689+ func : function, str
690+ Function to apply to each group. See the Notes section below for requirements.
656691
657- >>> grouped.transform("mean")
658- Falcon 370.0
659- Falcon 370.0
660- Parrot 25.0
661- Parrot 25.0
662- Name: Max Speed, dtype: float64
692+ Accepted inputs are:
663693
664- .. versionchanged:: 1.3.0
694+ - String
695+ - Python function
696+ - Numba JIT function with ``engine='numba'`` specified.
665697
666- The resulting dtype will reflect the return value of the passed ``func``,
667- for example:
698+ Only passing a single function is supported with this engine.
699+ If the ``'numba'`` engine is chosen, the function must be
700+ a user defined function with ``values`` and ``index`` as the
701+ first and second arguments respectively in the function signature.
702+ Each group's index will be passed to the user defined function
703+ and optionally available for use.
668704
669- >>> grouped.transform(lambda x: x.astype(int).max())
670- Falcon 390
671- Falcon 390
672- Parrot 30
673- Parrot 30
674- Name: Max Speed, dtype: int64
675- """
676- )
705+ If a string is chosen, then it needs to be the name
706+ of the groupby method you want to use.
707+ *args
708+ Positional arguments to pass to func.
709+ engine : str, default None
710+ * ``'cython'`` : Runs the function through C-extensions from cython.
711+ * ``'numba'`` : Runs the function through JIT compiled code from numba.
712+ * ``None`` : Defaults to ``'cython'`` or the global setting ``compute.use_numba``
677713
678- @Substitution (klass = "Series" , example = __examples_series_doc )
679- @Appender (_transform_template )
680- def transform (self , func , * args , engine = None , engine_kwargs = None , ** kwargs ):
714+ engine_kwargs : dict, default None
715+ * For ``'cython'`` engine, there are no accepted ``engine_kwargs``
716+ * For ``'numba'`` engine, the engine can accept ``nopython``, ``nogil``
717+ and ``parallel`` dictionary keys. The values must either be ``True`` or
718+ ``False``. The default ``engine_kwargs`` for the ``'numba'`` engine is
719+ ``{'nopython': True, 'nogil': False, 'parallel': False}`` and will be
720+ applied to the function
721+
722+ **kwargs
723+ Keyword arguments to be passed into func.
724+
725+ Returns
726+ -------
727+ Series
728+ Series with the same indexes as the original object filled
729+ with transformed values.
730+
731+ See Also
732+ --------
733+ Series.groupby.apply : Apply function ``func`` group-wise and combine
734+ the results together.
735+ Series.groupby.aggregate : Aggregate using one or more operations.
736+ Series.transform : Call ``func`` on self producing a Series with the
737+ same axis shape as self.
738+
739+ Notes
740+ -----
741+ Each group is endowed the attribute 'name' in case you need to know
742+ which group you are working on.
743+
744+ The current implementation imposes three requirements on f:
745+
746+ * f must return a value that either has the same shape as the input
747+ subframe or can be broadcast to the shape of the input subframe.
748+ For example, if `f` returns a scalar it will be broadcast to have the
749+ same shape as the input subframe.
750+ * if this is a DataFrame, f must support application column-by-column
751+ in the subframe. If f also supports application to the entire subframe,
752+ then a fast path is used starting from the second chunk.
753+ * f must not mutate groups. Mutation is not supported and may
754+ produce unexpected results. See :ref:`gotchas.udf-mutation` for more details.
755+
756+ When using ``engine='numba'``, there will be no "fall back" behavior internally.
757+ The group data and group index will be passed as numpy arrays to the JITed
758+ user defined function, and no alternative execution attempts will be tried.
759+
760+ .. versionchanged:: 1.3.0
761+
762+ The resulting dtype will reflect the return value of the passed ``func``,
763+ see the examples below.
764+
765+ .. versionchanged:: 2.0.0
766+
767+ When using ``.transform`` on a grouped DataFrame and the transformation function
768+ returns a DataFrame, pandas now aligns the result's index
769+ with the input's index. You can call ``.to_numpy()`` on the
770+ result of the transformation function to avoid alignment.
771+
772+ Examples
773+ --------
774+
775+ >>> ser = pd.Series([390.0, 350.0, 30.0, 20.0],
776+ ... index=["Falcon", "Falcon", "Parrot", "Parrot"],
777+ ... name="Max Speed")
778+ >>> grouped = ser.groupby([1, 1, 2, 2])
779+ >>> grouped.transform(lambda x: (x - x.mean()) / x.std())
780+ Falcon 0.707107
781+ Falcon -0.707107
782+ Parrot 0.707107
783+ Parrot -0.707107
784+ Name: Max Speed, dtype: float64
785+
786+ Broadcast result of the transformation
787+
788+ >>> grouped.transform(lambda x: x.max() - x.min())
789+ Falcon 40.0
790+ Falcon 40.0
791+ Parrot 10.0
792+ Parrot 10.0
793+ Name: Max Speed, dtype: float64
794+
795+ >>> grouped.transform("mean")
796+ Falcon 370.0
797+ Falcon 370.0
798+ Parrot 25.0
799+ Parrot 25.0
800+ Name: Max Speed, dtype: float64
801+
802+ .. versionchanged:: 1.3.0
803+
804+ The resulting dtype will reflect the return value of the passed ``func``,
805+ for example:
806+
807+ >>> grouped.transform(lambda x: x.astype(int).max())
808+ Falcon 390
809+ Falcon 390
810+ Parrot 30
811+ Parrot 30
812+ Name: Max Speed, dtype: int64
813+ """
681814 return self ._transform (
682815 func , * args , engine = engine , engine_kwargs = engine_kwargs , ** kwargs
683816 )
0 commit comments