diff --git a/pandas/core/frame.py b/pandas/core/frame.py index 6de808d9a73fa..251bc6587872d 100644 --- a/pandas/core/frame.py +++ b/pandas/core/frame.py @@ -5995,33 +5995,6 @@ def _aggregate(self, arg, axis=0, *args, **kwargs): agg = aggregate - _transform_doc = dedent(""" - Examples - -------- - >>> df = pd.DataFrame(np.random.randn(10, 2), columns=['A', 'B'], - ... index=pd.date_range('1/1/2000', periods=10)) - >>> df.iloc[3:7] = np.nan - - >>> df.transform(lambda x: (x - x.mean()) / x.std()) - A B - 2000-01-01 0.579457 1.236184 - 2000-01-02 0.370357 -0.605875 - 2000-01-03 1.455756 -0.277446 - 2000-01-04 NaN NaN - 2000-01-05 NaN NaN - 2000-01-06 NaN NaN - 2000-01-07 NaN NaN - 2000-01-08 -0.498658 1.274522 - 2000-01-09 -0.540524 -1.012676 - 2000-01-10 -1.366388 -0.614710 - - See also - -------- - pandas.DataFrame.aggregate - pandas.DataFrame.apply - """) - - @Appender(_transform_doc) @Appender(_shared_docs['transform'] % _shared_doc_kwargs) def transform(self, func, axis=0, *args, **kwargs): axis = self._get_axis_number(axis) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 23f14aaa02c75..10761ca5daf0b 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -4583,9 +4583,6 @@ def pipe(self, func, *args, **kwargs): func : function, string, list of string/functions or dictionary Function to use for transforming the data. If a function, must either work when passed a %(klass)s or when passed to %(klass)s.apply. - The function (or each function in a list/dict) must return an - object with the same length for the provided axis as the - calling %(klass)s. Accepted combinations are: @@ -4606,6 +4603,43 @@ def pipe(self, func, *args, **kwargs): Raises ------ ValueError: if the returned %(klass)s has a different length than self. + + Examples + -------- + >>> df = pd.DataFrame({'A': range(10), 'B': range(10, 0, -1)}, + ... index=pd.date_range('1/1/2000', periods=10)) + >>> df.iloc[3:7] = np.nan + + >>> df.transform(lambda x: (x - x.mean()) / x.std()) + A B + 2000-01-01 -1.143001 1.143001 + 2000-01-02 -0.889001 0.889001 + 2000-01-03 -0.635001 0.635001 + 2000-01-04 NaN NaN + 2000-01-05 NaN NaN + 2000-01-06 NaN NaN + 2000-01-07 NaN NaN + 2000-01-08 0.635001 -0.635001 + 2000-01-09 0.889001 -0.889001 + 2000-01-10 1.143001 -1.143001 + + It is only required for the axis specified in the ``axis`` parameter + to have the same length for output and for self. The other axis may have a + different length: + + >>> s = pd.Series(range(5)) + >>> s.transform([np.sqrt, np.exp]) + sqrt exp + 0 0.000000 1.000000 + 1 1.000000 2.718282 + 2 1.414214 7.389056 + 3 1.732051 20.085537 + 4 2.000000 54.598150 + + See also + -------- + pandas.%(klass)s.aggregate + pandas.%(klass)s.apply """) # ---------------------------------------------------------------------- diff --git a/pandas/core/series.py b/pandas/core/series.py index b8920619cf96b..cc22be9db95da 100644 --- a/pandas/core/series.py +++ b/pandas/core/series.py @@ -3098,33 +3098,6 @@ def aggregate(self, func, axis=0, *args, **kwargs): agg = aggregate - _transform_doc = dedent(""" - Examples - -------- - >>> s = pd.Series(range(5)) - >>> s.transform(lambda x: (x - x.mean()) / x.std()) - 0 -1.264911 - 1 -0.632456 - 2 0.000000 - 3 0.632456 - 4 1.264911 - dtype: float64 - - >>> s.transform([np.sqrt, np.exp]) - sqrt exp - 0 0.000000 1.000000 - 1 1.000000 2.718282 - 2 1.414214 7.389056 - 3 1.732051 20.085537 - 4 2.000000 54.598150 - - See also - -------- - pandas.Series.aggregate - pandas.Series.apply - """) - - @Appender(_transform_doc) @Appender(generic._shared_docs['transform'] % _shared_doc_kwargs) def transform(self, func, axis=0, *args, **kwargs): # Validate the axis parameter