diff --git a/pandas/core/apply.py b/pandas/core/apply.py index da049218d5187..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, func, 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"])) + >>> 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/arrays/datetimelike.py b/pandas/core/arrays/datetimelike.py index b8fca76115446..1e9b5641aa5e0 100644 --- a/pandas/core/arrays/datetimelike.py +++ b/pandas/core/arrays/datetimelike.py @@ -258,8 +258,9 @@ def _unbox_scalar( Examples -------- - >>> self._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) 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 98dc3a6b45c7b..821e41db6b065 100644 --- a/pandas/core/generic.py +++ b/pandas/core/generic.py @@ -2204,7 +2204,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 @@ -5864,9 +5864,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 @@ -5883,25 +5883,70 @@ def pipe( Notes ----- Use ``.pipe`` when chaining together functions that expect - Series, DataFrames or GroupBy objects. Instead of writing + Series, DataFrames or GroupBy objects. - >>> func(g(h(df), arg1=a), arg2=b, arg3=c) # doctest: +SKIP + Examples + -------- + Constructing a income DataFrame from a dictionary. + + >>> data = [[8000, 1000], [9500, np.nan], [5000, 2000]] + >>> df = pd.DataFrame(data, columns=['Salary', 'Others']) + >>> df + Salary Others + 0 8000 1000.0 + 1 9500 NaN + 2 5000 2000.0 + + Functions that perform tax reductions on an income DataFrame. + + >>> 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 df * (1 - new_rate) + + Instead of writing + + >>> subtract_national_insurance( + ... subtract_state_tax(subtract_federal_tax(df), 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) - ... ) # doctest: +SKIP + >>> ( + ... 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 + 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) - ... ) # doctest: +SKIP + data. For example, suppose ``national_insurance`` takes its data as ``df`` + in the second argument: + + >>> 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 + ... ) + ... ) + 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) diff --git a/pandas/io/formats/style_render.py b/pandas/io/formats/style_render.py index 3482e29fb1a70..c5262b9f52fc7 100644 --- a/pandas/io/formats/style_render.py +++ b/pandas/io/formats/style_render.py @@ -1272,7 +1272,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