From 3d5acdc59ab1661f040cb5fae3f7f1072e8bcf56 Mon Sep 17 00:00:00 2001 From: Su Date: Wed, 15 Feb 2023 02:22:42 +0800 Subject: [PATCH 1/9] Set value for undefined variables --- pandas/core/apply.py | 2 +- pandas/core/dtypes/base.py | 1 + pandas/core/generic.py | 11 ++++++++++- pandas/io/formats/style.py | 2 +- pandas/io/formats/style_render.py | 2 +- 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index c28da1bc758cd..f46c64a0d8660 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -1336,7 +1336,7 @@ def relabel_result( >>> funcs = {"A": ["max"], "C": ["max"], "B": ["mean", "min"]} >>> columns = ("foo", "aab", "bar", "dat") >>> order = [0, 1, 2, 3] - >>> _relabel_result(result, func, columns, order) # doctest: +SKIP + >>> relabel_result(result, funcs, columns, order) # doctest: +SKIP dict(A=Series([2.0, NaN, NaN, NaN], index=["foo", "aab", "bar", "dat"]), C=Series([NaN, 6.0, NaN, NaN], index=["foo", "aab", "bar", "dat"]), B=Series([NaN, NaN, 2.5, 4.0], index=["foo", "aab", "bar", "dat"])) diff --git a/pandas/core/dtypes/base.py b/pandas/core/dtypes/base.py index bce2a82f057f3..65a47452b1fe8 100644 --- a/pandas/core/dtypes/base.py +++ b/pandas/core/dtypes/base.py @@ -261,6 +261,7 @@ def construct_from_string( For extension dtypes with arguments the following may be an adequate implementation. + >>> import re >>> @classmethod ... def construct_from_string(cls, string): ... pattern = re.compile(r"^my_type\[(?P.+)\]$") diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 5d2a0fe66cc1d..760f935ae2079 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2209,7 +2209,7 @@ def to_excel( >>> with pd.ExcelWriter('output.xlsx', ... mode='a') as writer: # doctest: +SKIP - ... df.to_excel(writer, sheet_name='Sheet_name_3') + ... df1.to_excel(writer, sheet_name='Sheet_name_3') To set the library that is used to write the Excel file, you can pass the `engine` keyword (the default engine is @@ -5894,6 +5894,15 @@ def pipe( Use ``.pipe`` when chaining together functions that expect Series, DataFrames or GroupBy objects. Instead of writing + >>> df = pd.Dataframe(np.random.rand(2, 2)) + >>> h = lambda df: [x.lower() for x in df.columns] + >>> def g(x, arg1): + ... return x * arg1 + >>> def func(x, arg2, arg3): + ... return x * arg2 * arg3 + >>> a = 1 + >>> b = 2 + >>> c = 3 >>> func(g(h(df), arg1=a), arg2=b, arg3=c) # doctest: +SKIP You can write diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 442f2ab72a1e2..222ec25011354 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1870,7 +1870,7 @@ def apply_index( >>> df = pd.DataFrame([[1,2], [3,4]], index=["A", "B"]) >>> def color_b(s): - ... return {ret} + ... return '{ret}' >>> df.style.{this}_index(color_b) # doctest: +SKIP .. figure:: ../../_static/style/appmaphead1.png diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 5264342661b3f..9bbc9236ec555 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -1276,7 +1276,7 @@ def format_index( >>> df = pd.DataFrame([[1, 2, 3]], ... columns=pd.MultiIndex.from_arrays([["a", "a", "b"],[2, np.nan, 4]])) - >>> df.style.format_index({0: lambda v: upper(v)}, axis=1, precision=1) + >>> df.style.format_index({0: lambda v: v.upper()}, axis=1, precision=1) ... # doctest: +SKIP A B 2.0 nan 4.0 From 44b21792f3286aca273e3b35600310d95c21cb09 Mon Sep 17 00:00:00 2001 From: Su Date: Thu, 16 Feb 2023 09:53:35 +0800 Subject: [PATCH 2/9] Update example for pipe --- pandas/core/arrays/datetimelike.py | 6 ++- pandas/core/generic.py | 65 +++++++++++++++++++++--------- pandas/io/formats/style.py | 2 +- 3 files changed, 51 insertions(+), 22 deletions(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index 437195bbcf7e9..91b8164cf12bd 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -259,7 +259,11 @@ def _unbox_scalar( Examples -------- - >>> self._unbox_scalar(Timedelta("10s")) # doctest: +SKIP + >>> obj = DatetimeLikeArrayMixin( + ... OpsMixin(), + ... NDArrayBackedExtensionArray(NDArrayBacked(), ExtensionArray()) + ... ) + >>> obj._unbox_scalar(Timedelta("10s")) # doctest: +SKIP 10000000000 """ raise AbstractMethodError(self) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 760f935ae2079..45c2a616c847c 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5892,33 +5892,58 @@ def pipe( Notes ----- Use ``.pipe`` when chaining together functions that expect - Series, DataFrames or GroupBy objects. Instead of writing - - >>> df = pd.Dataframe(np.random.rand(2, 2)) - >>> h = lambda df: [x.lower() for x in df.columns] - >>> def g(x, arg1): - ... return x * arg1 - >>> def func(x, arg2, arg3): - ... return x * arg2 * arg3 - >>> a = 1 - >>> b = 2 - >>> c = 3 - >>> func(g(h(df), arg1=a), arg2=b, arg3=c) # doctest: +SKIP + Series, DataFrames or GroupBy objects. + + Examples + -------- + Constructing a income DataFrame from a dictionary. + + >>> data = [[8000, 1000], [9500, np.nan], [5000, 2000]] + >>> income = pd.DataFrame(data, columns=['Salary', 'Others']) + Salary Others + 0 8000 1000.0 + 1 9500 NaN + 2 5000 2000.0 + + Functions that perform tax reductions on an income DataFrame. + + >>> def federal_tax(income): + ... return income * 0.9 + >>> def state_tax(income, rate): + ... return income * (1 - rate) + >>> def national_insurance(income, rate, rate_increase): + ... new_rate = rate + rate_increase + ... return income * (1 - new_rate) + + Instead of writing + + >>> (national_insurance(state_tax(federal_tax(income), rate=0.12), + ... rate=0.05, + ... rate_increase=0.02) + ... )# doctest: +SKIP You can write - >>> (df.pipe(h) - ... .pipe(g, arg1=a) - ... .pipe(func, arg2=b, arg3=c) + >>> (income.pipe(federal_tax) + ... .pipe(state_tax, rate=0.12) + ... .pipe(national_insurance, rate=0.05, rate_increase=0.02) ... ) # doctest: +SKIP + Salary Others + 0 5892.48 736.56 + 1 6997.32 NaN + 2 3682.80 1473.12 If you have a function that takes the data as (say) the second argument, pass a tuple indicating which keyword expects the - data. For example, suppose ``func`` takes its data as ``arg2``: - - >>> (df.pipe(h) - ... .pipe(g, arg1=a) - ... .pipe((func, 'arg2'), arg1=a, arg3=c) + data. For example, suppose ``national_insurance`` takes its data as ``income`` + in the second argument: + + >>> def national_insurance(rate, income, rate_increase): + ... new_rate = rate + rate_increase + ... return income * (1 - new_rate) + >>> (income.pipe(federal_tax) + ... .pipe(state_tax, rate=0.12) + ... .pipe((national_insurance, 'income'), rate=0.05, rate_increase=0.02) ... ) # doctest: +SKIP """ if using_copy_on_write(): diff --git a/pandas/io/formats/style.py b/pandas/io/formats/style.py index 222ec25011354..442f2ab72a1e2 100644 --- a/pandas/io/formats/style.py +++ b/pandas/io/formats/style.py @@ -1870,7 +1870,7 @@ def apply_index( >>> df = pd.DataFrame([[1,2], [3,4]], index=["A", "B"]) >>> def color_b(s): - ... return '{ret}' + ... return {ret} >>> df.style.{this}_index(color_b) # doctest: +SKIP .. figure:: ../../_static/style/appmaphead1.png From dc1722f781d4ab8abf3a4f2e91e6ef4bc3d3f323 Mon Sep 17 00:00:00 2001 From: Su Date: Mon, 20 Feb 2023 22:28:58 +0800 Subject: [PATCH 3/9] Update generic.py --- pandas/core/generic.py | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index ce8598502638a..08da168cc1fda 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5866,9 +5866,9 @@ def pipe( Alternatively a ``(callable, data_keyword)`` tuple where ``data_keyword`` is a string indicating the keyword of ``callable`` that expects the {klass}. - args : iterable, optional + *args : iterable, optional Positional arguments passed into ``func``. - kwargs : mapping, optional + **kwargs : mapping, optional A dictionary of keyword arguments passed into ``func``. Returns @@ -5893,6 +5893,7 @@ def pipe( >>> data = [[8000, 1000], [9500, np.nan], [5000, 2000]] >>> income = pd.DataFrame(data, columns=['Salary', 'Others']) + >>> income Salary Others 0 8000 1000.0 1 9500 NaN @@ -5911,16 +5912,14 @@ def pipe( Instead of writing >>> (national_insurance(state_tax(federal_tax(income), rate=0.12), - ... rate=0.05, - ... rate_increase=0.02) - ... )# doctest: +SKIP + ... rate=0.05, + ... rate_increase=0.02)) # doctest: +SKIP You can write >>> (income.pipe(federal_tax) - ... .pipe(state_tax, rate=0.12) - ... .pipe(national_insurance, rate=0.05, rate_increase=0.02) - ... ) # doctest: +SKIP + ... .pipe(state_tax, rate=0.12) + ... .pipe(national_insurance, rate=0.05, rate_increase=0.02)) Salary Others 0 5892.48 736.56 1 6997.32 NaN @@ -5935,9 +5934,12 @@ def pipe( ... new_rate = rate + rate_increase ... return income * (1 - new_rate) >>> (income.pipe(federal_tax) - ... .pipe(state_tax, rate=0.12) - ... .pipe((national_insurance, 'income'), rate=0.05, rate_increase=0.02) - ... ) # doctest: +SKIP + ... .pipe(state_tax, rate=0.12) + ... .pipe((national_insurance, 'income'), rate=0.05, rate_increase=0.02)) + Salary Others + 0 5892.48 736.56 + 1 6997.32 NaN + 2 3682.80 1473.12 """ if using_copy_on_write(): return common.pipe(self.copy(deep=None), func, *args, **kwargs) From 7104f8b308a9324934b489d6795ce4f3add7d784 Mon Sep 17 00:00:00 2001 From: Su Date: Mon, 20 Feb 2023 22:38:38 +0800 Subject: [PATCH 4/9] Update _unbox_scalar example --- pandas/core/arrays/datetimelike.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/pandas/core/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index b0685d2a5403e..1e9b5641aa5e0 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -258,12 +258,9 @@ def _unbox_scalar( Examples -------- - >>> obj = DatetimeLikeArrayMixin( - ... OpsMixin(), - ... NDArrayBackedExtensionArray(NDArrayBacked(), ExtensionArray()) - ... ) - >>> obj._unbox_scalar(Timedelta("10s")) # doctest: +SKIP - 10000000000 + >>> arr = pd.arrays.DatetimeArray(np.array(['1970-01-01'], 'datetime64[ns]')) + >>> arr._unbox_scalar(arr[0]) + numpy.datetime64('1970-01-01T00:00:00.000000000') """ raise AbstractMethodError(self) From f115c0d7e6404e1d0ffbbf32964be51ac53da7d9 Mon Sep 17 00:00:00 2001 From: Su Date: Wed, 22 Feb 2023 19:59:57 +0800 Subject: [PATCH 5/9] Update indentation --- pandas/core/apply.py | 2 +- pandas/core/generic.py | 28 +++++++++++++++------------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index db17a7c3e129a..c0bade758a613 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -1330,7 +1330,7 @@ def relabel_result( >>> funcs = {"A": ["max"], "C": ["max"], "B": ["mean", "min"]} >>> columns = ("foo", "aab", "bar", "dat") >>> order = [0, 1, 2, 3] - >>> relabel_result(result, funcs, columns, order) # doctest: +SKIP + >>> relabel_result(result, funcs, columns, order) dict(A=Series([2.0, NaN, NaN, NaN], index=["foo", "aab", "bar", "dat"]), C=Series([NaN, 6.0, NaN, NaN], index=["foo", "aab", "bar", "dat"]), B=Series([NaN, NaN, 2.5, 4.0], index=["foo", "aab", "bar", "dat"])) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 08da168cc1fda..d75487c50cf1b 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5901,25 +5901,26 @@ def pipe( Functions that perform tax reductions on an income DataFrame. - >>> def federal_tax(income): + >>> def subtract_federal_tax(income): ... return income * 0.9 - >>> def state_tax(income, rate): + >>> def subtract_state_tax(income, rate): ... return income * (1 - rate) - >>> def national_insurance(income, rate, rate_increase): + >>> def subtract_national_insurance(income, rate, rate_increase): ... new_rate = rate + rate_increase ... return income * (1 - new_rate) Instead of writing - >>> (national_insurance(state_tax(federal_tax(income), rate=0.12), - ... rate=0.05, - ... rate_increase=0.02)) # doctest: +SKIP + >>> (subtract_national_insurance( + ... subtract_state_tax(subtract_federal_tax(income),rate=0.12), + ... rate=0.05, + ... rate_increase=0.02)) # doctest: +SKIP You can write - >>> (income.pipe(federal_tax) - ... .pipe(state_tax, rate=0.12) - ... .pipe(national_insurance, rate=0.05, rate_increase=0.02)) + >>> (income.pipe(subtract_federal_tax) + ... .pipe(subtract_state_tax, rate=0.12) + ... .pipe(subtract_national_insurance, rate=0.05, rate_increase=0.02)) Salary Others 0 5892.48 736.56 1 6997.32 NaN @@ -5930,12 +5931,13 @@ def pipe( data. For example, suppose ``national_insurance`` takes its data as ``income`` in the second argument: - >>> def national_insurance(rate, income, rate_increase): + >>> def subtract_national_insurance(rate, income, rate_increase): ... new_rate = rate + rate_increase ... return income * (1 - new_rate) - >>> (income.pipe(federal_tax) - ... .pipe(state_tax, rate=0.12) - ... .pipe((national_insurance, 'income'), rate=0.05, rate_increase=0.02)) + >>> (income.pipe(subtract_federal_tax) + ... .pipe(subtract_state_tax, rate=0.12) + ... .pipe((subtract_national_insurance, 'income'), + ... rate=0.05, rate_increase=0.02)) Salary Others 0 5892.48 736.56 1 6997.32 NaN From eec26116271b3a721c1850c36fb0a8d00fe930c5 Mon Sep 17 00:00:00 2001 From: Su Date: Sat, 25 Feb 2023 23:46:29 +0800 Subject: [PATCH 6/9] Update dataframe name --- pandas/core/generic.py | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index d75487c50cf1b..ff7b5b0090583 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5892,8 +5892,8 @@ def pipe( Constructing a income DataFrame from a dictionary. >>> data = [[8000, 1000], [9500, np.nan], [5000, 2000]] - >>> income = pd.DataFrame(data, columns=['Salary', 'Others']) - >>> income + >>> df = pd.DataFrame(data, columns=['Salary', 'Others']) + >>> df Salary Others 0 8000 1000.0 1 9500 NaN @@ -5901,24 +5901,24 @@ def pipe( Functions that perform tax reductions on an income DataFrame. - >>> def subtract_federal_tax(income): - ... return income * 0.9 - >>> def subtract_state_tax(income, rate): - ... return income * (1 - rate) - >>> def subtract_national_insurance(income, rate, rate_increase): + >>> def subtract_federal_tax(df): + ... return df * 0.9 + >>> def subtract_state_tax(df, rate): + ... return df * (1 - rate) + >>> def subtract_national_insurance(df, rate, rate_increase): ... new_rate = rate + rate_increase - ... return income * (1 - new_rate) + ... return df * (1 - new_rate) Instead of writing >>> (subtract_national_insurance( - ... subtract_state_tax(subtract_federal_tax(income),rate=0.12), + ... subtract_state_tax(subtract_federal_tax(df),rate=0.12), ... rate=0.05, ... rate_increase=0.02)) # doctest: +SKIP You can write - >>> (income.pipe(subtract_federal_tax) + >>> (df.pipe(subtract_federal_tax) ... .pipe(subtract_state_tax, rate=0.12) ... .pipe(subtract_national_insurance, rate=0.05, rate_increase=0.02)) Salary Others @@ -5928,15 +5928,15 @@ def pipe( If you have a function that takes the data as (say) the second argument, pass a tuple indicating which keyword expects the - data. For example, suppose ``national_insurance`` takes its data as ``income`` + data. For example, suppose ``national_insurance`` takes its data as ``df`` in the second argument: - >>> def subtract_national_insurance(rate, income, rate_increase): + >>> def subtract_national_insurance(rate, df, rate_increase): ... new_rate = rate + rate_increase - ... return income * (1 - new_rate) - >>> (income.pipe(subtract_federal_tax) + ... return df * (1 - new_rate) + >>> (df.pipe(subtract_federal_tax) ... .pipe(subtract_state_tax, rate=0.12) - ... .pipe((subtract_national_insurance, 'income'), + ... .pipe((subtract_national_insurance, 'df'), ... rate=0.05, rate_increase=0.02)) Salary Others 0 5892.48 736.56 From 0032da4348dbfd0f247ff3e60feb484f077364ba Mon Sep 17 00:00:00 2001 From: Su Date: Sun, 26 Feb 2023 00:22:15 +0800 Subject: [PATCH 7/9] Fix indentation --- pandas/core/generic.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index f70651d066ca3..0e54cac7774c9 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5917,8 +5917,8 @@ def pipe( You can write >>> (df.pipe(subtract_federal_tax) - ... .pipe(subtract_state_tax, rate=0.12) - ... .pipe(subtract_national_insurance, rate=0.05, rate_increase=0.02)) + ... .pipe(subtract_state_tax, rate=0.12) + ... .pipe(subtract_national_insurance, rate=0.05, rate_increase=0.02)) Salary Others 0 5892.48 736.56 1 6997.32 NaN @@ -5933,9 +5933,9 @@ def pipe( ... new_rate = rate + rate_increase ... return df * (1 - new_rate) >>> (df.pipe(subtract_federal_tax) - ... .pipe(subtract_state_tax, rate=0.12) - ... .pipe((subtract_national_insurance, 'df'), - ... rate=0.05, rate_increase=0.02)) + ... .pipe(subtract_state_tax, rate=0.12) + ... .pipe((subtract_national_insurance, 'df'), + ... rate=0.05, rate_increase=0.02)) Salary Others 0 5892.48 736.56 1 6997.32 NaN From c79bfda679205e100e55f43fb0f3a433219c5292 Mon Sep 17 00:00:00 2001 From: Su Date: Sun, 26 Feb 2023 11:48:57 +0800 Subject: [PATCH 8/9] Remove extra parentheses --- pandas/core/generic.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/pandas/core/generic.py b/pandas/core/generic.py index 0e54cac7774c9..1f708e421b3ef 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5909,16 +5909,18 @@ def pipe( Instead of writing - >>> (subtract_national_insurance( + >>> subtract_national_insurance( ... subtract_state_tax(subtract_federal_tax(df),rate=0.12), ... rate=0.05, - ... rate_increase=0.02)) # doctest: +SKIP + ... rate_increase=0.02) # doctest: +SKIP You can write - >>> (df.pipe(subtract_federal_tax) - ... .pipe(subtract_state_tax, rate=0.12) - ... .pipe(subtract_national_insurance, rate=0.05, rate_increase=0.02)) + >>> df.pipe( + ... subtract_federal_tax + ... ).pipe( + ... subtract_state_tax, rate=0.12 + ... ).pipe(subtract_national_insurance, rate=0.05, rate_increase=0.02) Salary Others 0 5892.48 736.56 1 6997.32 NaN @@ -5932,10 +5934,12 @@ def pipe( >>> def subtract_national_insurance(rate, df, rate_increase): ... new_rate = rate + rate_increase ... return df * (1 - new_rate) - >>> (df.pipe(subtract_federal_tax) - ... .pipe(subtract_state_tax, rate=0.12) - ... .pipe((subtract_national_insurance, 'df'), - ... rate=0.05, rate_increase=0.02)) + >>> df.pipe( + ... subtract_federal_tax + ... ).pipe( + ... subtract_state_tax, rate=0.12 + ... ).pipe((subtract_national_insurance, 'df'), + ... rate=0.05, rate_increase=0.02) Salary Others 0 5892.48 736.56 1 6997.32 NaN From 3384a7fb75776e6ad62601d5c104ed34c7b06a56 Mon Sep 17 00:00:00 2001 From: MarcoGorelli <> Date: Tue, 28 Feb 2023 11:02:20 +0000 Subject: [PATCH 9/9] fixup relabel_result, restore chained method syntax --- pandas/core/apply.py | 22 ++++++++++++++-------- pandas/core/generic.py | 27 +++++++++++++++------------ 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/pandas/core/apply.py b/pandas/core/apply.py index c0bade758a613..4790d00a071cb 100644 --- a/pandas/core/apply.py +++ b/pandas/core/apply.py @@ -1323,17 +1323,23 @@ def relabel_result( columns: New columns name for relabelling order: New order for relabelling - Examples: - --------- - >>> result = DataFrame({"A": [np.nan, 2, np.nan], - ... "C": [6, np.nan, np.nan], "B": [np.nan, 4, 2.5]}) # doctest: +SKIP + Examples + -------- + >>> from pandas.core.apply import relabel_result + >>> result = pd.DataFrame( + ... {"A": [np.nan, 2, np.nan], "C": [6, np.nan, np.nan], "B": [np.nan, 4, 2.5]}, + ... index=["max", "mean", "min"] + ... ) >>> funcs = {"A": ["max"], "C": ["max"], "B": ["mean", "min"]} >>> columns = ("foo", "aab", "bar", "dat") >>> order = [0, 1, 2, 3] - >>> relabel_result(result, funcs, columns, order) - dict(A=Series([2.0, NaN, NaN, NaN], index=["foo", "aab", "bar", "dat"]), - C=Series([NaN, 6.0, NaN, NaN], index=["foo", "aab", "bar", "dat"]), - B=Series([NaN, NaN, 2.5, 4.0], index=["foo", "aab", "bar", "dat"])) + >>> result_in_dict = relabel_result(result, funcs, columns, order) + >>> pd.DataFrame(result_in_dict, index=columns) + A C B + foo 2.0 NaN NaN + aab NaN 6.0 NaN + bar NaN NaN 4.0 + dat NaN NaN 2.5 """ from pandas.core.indexes.base import Index diff --git a/pandas/core/generic.py b/pandas/core/generic.py index b3864db08ce37..821e41db6b065 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -5910,17 +5910,17 @@ def pipe( Instead of writing >>> subtract_national_insurance( - ... subtract_state_tax(subtract_federal_tax(df),rate=0.12), + ... subtract_state_tax(subtract_federal_tax(df), rate=0.12), ... rate=0.05, ... rate_increase=0.02) # doctest: +SKIP You can write - >>> df.pipe( - ... subtract_federal_tax - ... ).pipe( - ... subtract_state_tax, rate=0.12 - ... ).pipe(subtract_national_insurance, rate=0.05, rate_increase=0.02) + >>> ( + ... df.pipe(subtract_federal_tax) + ... .pipe(subtract_state_tax, rate=0.12) + ... .pipe(subtract_national_insurance, rate=0.05, rate_increase=0.02) + ... ) Salary Others 0 5892.48 736.56 1 6997.32 NaN @@ -5934,12 +5934,15 @@ def pipe( >>> def subtract_national_insurance(rate, df, rate_increase): ... new_rate = rate + rate_increase ... return df * (1 - new_rate) - >>> df.pipe( - ... subtract_federal_tax - ... ).pipe( - ... subtract_state_tax, rate=0.12 - ... ).pipe((subtract_national_insurance, 'df'), - ... rate=0.05, rate_increase=0.02) + >>> ( + ... df.pipe(subtract_federal_tax) + ... .pipe(subtract_state_tax, rate=0.12) + ... .pipe( + ... (subtract_national_insurance, 'df'), + ... rate=0.05, + ... rate_increase=0.02 + ... ) + ... ) Salary Others 0 5892.48 736.56 1 6997.32 NaN