Skip to content

Commit b02728c

Browse files
authored
DOC: Set value for undefined variables in examples (#51389)
* Set value for undefined variables * Update example for pipe * Update generic.py * Update _unbox_scalar example * Update indentation * Update dataframe name * Fix indentation * Remove extra parentheses * fixup relabel_result, restore chained method syntax --------- Co-authored-by: MarcoGorelli <>
1 parent 132fca0 commit b02728c

File tree

5 files changed

+79
-26
lines changed

5 files changed

+79
-26
lines changed

pandas/core/apply.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -1323,17 +1323,23 @@ def relabel_result(
13231323
columns: New columns name for relabelling
13241324
order: New order for relabelling
13251325
1326-
Examples:
1327-
---------
1328-
>>> result = DataFrame({"A": [np.nan, 2, np.nan],
1329-
... "C": [6, np.nan, np.nan], "B": [np.nan, 4, 2.5]}) # doctest: +SKIP
1326+
Examples
1327+
--------
1328+
>>> from pandas.core.apply import relabel_result
1329+
>>> result = pd.DataFrame(
1330+
... {"A": [np.nan, 2, np.nan], "C": [6, np.nan, np.nan], "B": [np.nan, 4, 2.5]},
1331+
... index=["max", "mean", "min"]
1332+
... )
13301333
>>> funcs = {"A": ["max"], "C": ["max"], "B": ["mean", "min"]}
13311334
>>> columns = ("foo", "aab", "bar", "dat")
13321335
>>> order = [0, 1, 2, 3]
1333-
>>> _relabel_result(result, func, columns, order) # doctest: +SKIP
1334-
dict(A=Series([2.0, NaN, NaN, NaN], index=["foo", "aab", "bar", "dat"]),
1335-
C=Series([NaN, 6.0, NaN, NaN], index=["foo", "aab", "bar", "dat"]),
1336-
B=Series([NaN, NaN, 2.5, 4.0], index=["foo", "aab", "bar", "dat"]))
1336+
>>> result_in_dict = relabel_result(result, funcs, columns, order)
1337+
>>> pd.DataFrame(result_in_dict, index=columns)
1338+
A C B
1339+
foo 2.0 NaN NaN
1340+
aab NaN 6.0 NaN
1341+
bar NaN NaN 4.0
1342+
dat NaN NaN 2.5
13371343
"""
13381344
from pandas.core.indexes.base import Index
13391345

pandas/core/arrays/datetimelike.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,9 @@ def _unbox_scalar(
258258
259259
Examples
260260
--------
261-
>>> self._unbox_scalar(Timedelta("10s")) # doctest: +SKIP
262-
10000000000
261+
>>> arr = pd.arrays.DatetimeArray(np.array(['1970-01-01'], 'datetime64[ns]'))
262+
>>> arr._unbox_scalar(arr[0])
263+
numpy.datetime64('1970-01-01T00:00:00.000000000')
263264
"""
264265
raise AbstractMethodError(self)
265266

pandas/core/dtypes/base.py

+1
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,7 @@ def construct_from_string(
261261
For extension dtypes with arguments the following may be an
262262
adequate implementation.
263263
264+
>>> import re
264265
>>> @classmethod
265266
... def construct_from_string(cls, string):
266267
... pattern = re.compile(r"^my_type\[(?P<arg_name>.+)\]$")

pandas/core/generic.py

+60-15
Original file line numberDiff line numberDiff line change
@@ -2204,7 +2204,7 @@ def to_excel(
22042204
22052205
>>> with pd.ExcelWriter('output.xlsx',
22062206
... mode='a') as writer: # doctest: +SKIP
2207-
... df.to_excel(writer, sheet_name='Sheet_name_3')
2207+
... df1.to_excel(writer, sheet_name='Sheet_name_3')
22082208
22092209
To set the library that is used to write the Excel file,
22102210
you can pass the `engine` keyword (the default engine is
@@ -5864,9 +5864,9 @@ def pipe(
58645864
Alternatively a ``(callable, data_keyword)`` tuple where
58655865
``data_keyword`` is a string indicating the keyword of
58665866
``callable`` that expects the {klass}.
5867-
args : iterable, optional
5867+
*args : iterable, optional
58685868
Positional arguments passed into ``func``.
5869-
kwargs : mapping, optional
5869+
**kwargs : mapping, optional
58705870
A dictionary of keyword arguments passed into ``func``.
58715871
58725872
Returns
@@ -5883,25 +5883,70 @@ def pipe(
58835883
Notes
58845884
-----
58855885
Use ``.pipe`` when chaining together functions that expect
5886-
Series, DataFrames or GroupBy objects. Instead of writing
5886+
Series, DataFrames or GroupBy objects.
58875887
5888-
>>> func(g(h(df), arg1=a), arg2=b, arg3=c) # doctest: +SKIP
5888+
Examples
5889+
--------
5890+
Constructing a income DataFrame from a dictionary.
5891+
5892+
>>> data = [[8000, 1000], [9500, np.nan], [5000, 2000]]
5893+
>>> df = pd.DataFrame(data, columns=['Salary', 'Others'])
5894+
>>> df
5895+
Salary Others
5896+
0 8000 1000.0
5897+
1 9500 NaN
5898+
2 5000 2000.0
5899+
5900+
Functions that perform tax reductions on an income DataFrame.
5901+
5902+
>>> def subtract_federal_tax(df):
5903+
... return df * 0.9
5904+
>>> def subtract_state_tax(df, rate):
5905+
... return df * (1 - rate)
5906+
>>> def subtract_national_insurance(df, rate, rate_increase):
5907+
... new_rate = rate + rate_increase
5908+
... return df * (1 - new_rate)
5909+
5910+
Instead of writing
5911+
5912+
>>> subtract_national_insurance(
5913+
... subtract_state_tax(subtract_federal_tax(df), rate=0.12),
5914+
... rate=0.05,
5915+
... rate_increase=0.02) # doctest: +SKIP
58895916
58905917
You can write
58915918
5892-
>>> (df.pipe(h)
5893-
... .pipe(g, arg1=a)
5894-
... .pipe(func, arg2=b, arg3=c)
5895-
... ) # doctest: +SKIP
5919+
>>> (
5920+
... df.pipe(subtract_federal_tax)
5921+
... .pipe(subtract_state_tax, rate=0.12)
5922+
... .pipe(subtract_national_insurance, rate=0.05, rate_increase=0.02)
5923+
... )
5924+
Salary Others
5925+
0 5892.48 736.56
5926+
1 6997.32 NaN
5927+
2 3682.80 1473.12
58965928
58975929
If you have a function that takes the data as (say) the second
58985930
argument, pass a tuple indicating which keyword expects the
5899-
data. For example, suppose ``func`` takes its data as ``arg2``:
5900-
5901-
>>> (df.pipe(h)
5902-
... .pipe(g, arg1=a)
5903-
... .pipe((func, 'arg2'), arg1=a, arg3=c)
5904-
... ) # doctest: +SKIP
5931+
data. For example, suppose ``national_insurance`` takes its data as ``df``
5932+
in the second argument:
5933+
5934+
>>> def subtract_national_insurance(rate, df, rate_increase):
5935+
... new_rate = rate + rate_increase
5936+
... return df * (1 - new_rate)
5937+
>>> (
5938+
... df.pipe(subtract_federal_tax)
5939+
... .pipe(subtract_state_tax, rate=0.12)
5940+
... .pipe(
5941+
... (subtract_national_insurance, 'df'),
5942+
... rate=0.05,
5943+
... rate_increase=0.02
5944+
... )
5945+
... )
5946+
Salary Others
5947+
0 5892.48 736.56
5948+
1 6997.32 NaN
5949+
2 3682.80 1473.12
59055950
"""
59065951
if using_copy_on_write():
59075952
return common.pipe(self.copy(deep=None), func, *args, **kwargs)

pandas/io/formats/style_render.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1272,7 +1272,7 @@ def format_index(
12721272
12731273
>>> df = pd.DataFrame([[1, 2, 3]],
12741274
... columns=pd.MultiIndex.from_arrays([["a", "a", "b"],[2, np.nan, 4]]))
1275-
>>> df.style.format_index({0: lambda v: upper(v)}, axis=1, precision=1)
1275+
>>> df.style.format_index({0: lambda v: v.upper()}, axis=1, precision=1)
12761276
... # doctest: +SKIP
12771277
A B
12781278
2.0 nan 4.0

0 commit comments

Comments
 (0)