Skip to content

DOC: Fix some doctest errors #50494

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 18 additions & 16 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3477,7 +3477,7 @@ def transpose(self, *args, copy: bool = False) -> DataFrame:
0 1 3
1 2 4

>>> df1_transposed = df1.T # or df1.transpose()
>>> df1_transposed = df1.T # or df1.transpose()
>>> df1_transposed
0 1
col1 1 2
Expand Down Expand Up @@ -3507,7 +3507,7 @@ def transpose(self, *args, copy: bool = False) -> DataFrame:
0 Alice 9.5 False 0
1 Bob 8.0 True 0

>>> df2_transposed = df2.T # or df2.transpose()
>>> df2_transposed = df2.T # or df2.transpose()
>>> df2_transposed
0 1
name Alice Bob
Expand Down Expand Up @@ -4744,7 +4744,7 @@ def assign(self, **kwargs) -> DataFrame:
of the columns depends on another one defined within the same assign:

>>> df.assign(temp_f=lambda x: x['temp_c'] * 9 / 5 + 32,
... temp_k=lambda x: (x['temp_f'] + 459.67) * 5 / 9)
... temp_k=lambda x: (x['temp_f'] + 459.67) * 5 / 9)
temp_c temp_f temp_k
Portland 17.0 62.6 290.15
Berkeley 25.0 77.0 298.15
Expand Down Expand Up @@ -5983,8 +5983,8 @@ class max_speed
>>> columns = pd.MultiIndex.from_tuples([('speed', 'max'),
... ('species', 'type')])
>>> df = pd.DataFrame([(389.0, 'fly'),
... ( 24.0, 'fly'),
... ( 80.5, 'run'),
... (24.0, 'fly'),
... (80.5, 'run'),
... (np.nan, 'jump')],
... index=index,
... columns=columns)
Expand Down Expand Up @@ -8262,14 +8262,14 @@ def groupby(
4 2 1 1 5 4
5 2 2 2 6 5

>>> df.pivot(index="lev1", columns=["lev2", "lev3"],values="values")
>>> df.pivot(index="lev1", columns=["lev2", "lev3"], values="values")
lev2 1 2
lev3 1 2 1 2
lev1
1 0.0 1.0 2.0 NaN
2 4.0 3.0 NaN 5.0

>>> df.pivot(index=["lev1", "lev2"], columns=["lev3"],values="values")
>>> df.pivot(index=["lev1", "lev2"], columns=["lev3"], values="values")
lev3 1 2
lev1 lev2
1 1 0.0 1.0
Expand Down Expand Up @@ -8317,7 +8317,8 @@ def pivot(

Parameters
----------%s
values : column to aggregate, optional
values : list-like or scalar, optional
Column or columns to aggregate.
index : column, Grouper, array, or list of the previous
If an array is passed, it must be the same length as the data. The
list can contain any of the other types (except list).
Expand Down Expand Up @@ -8403,7 +8404,7 @@ def pivot(
This first example aggregates values by taking the sum.

>>> table = pd.pivot_table(df, values='D', index=['A', 'B'],
... columns=['C'], aggfunc=np.sum)
... columns=['C'], aggfunc=np.sum)
>>> table
C large small
A B
Expand All @@ -8415,7 +8416,7 @@ def pivot(
We can also fill missing values using the `fill_value` parameter.

>>> table = pd.pivot_table(df, values='D', index=['A', 'B'],
... columns=['C'], aggfunc=np.sum, fill_value=0)
... columns=['C'], aggfunc=np.sum, fill_value=0)
>>> table
C large small
A B
Expand All @@ -8427,8 +8428,7 @@ def pivot(
The next example aggregates by taking the mean across multiple columns.

>>> table = pd.pivot_table(df, values=['D', 'E'], index=['A', 'C'],
... aggfunc={'D': np.mean,
... 'E': np.mean})
... aggfunc={'D': np.mean, 'E': np.mean})
>>> table
D E
A C
Expand All @@ -8441,8 +8441,8 @@ def pivot(
value column.

>>> table = pd.pivot_table(df, values=['D', 'E'], index=['A', 'C'],
... aggfunc={'D': np.mean,
... 'E': [min, max, np.mean]})
... aggfunc={'D': np.mean,
... 'E': [min, max, np.mean]})
>>> table
D E
mean max mean min
Expand Down Expand Up @@ -10926,7 +10926,8 @@ def to_timestamp(

Returns
-------
DataFrame with DatetimeIndex
DataFrame
The DataFrame has a DatetimeIndex.
"""
new_obj = self.copy(deep=copy)

Expand Down Expand Up @@ -10960,7 +10961,8 @@ def to_period(

Returns
-------
DataFrame with PeriodIndex
DataFrame:
The DataFrame has a PeriodIndex.

Examples
--------
Expand Down
59 changes: 33 additions & 26 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -4846,8 +4846,8 @@ def sort_values(
4 96hr 50
>>> from natsort import index_natsorted
>>> df.sort_values(
... by="time",
... key=lambda x: np.argsort(index_natsorted(df["time"]))
... by="time",
... key=lambda x: np.argsort(index_natsorted(df["time"]))
... )
time value
0 0hr 10
Expand Down Expand Up @@ -7694,10 +7694,10 @@ def isna(self: NDFrameT) -> NDFrameT:
Show which entries in a DataFrame are NA.

>>> df = pd.DataFrame(dict(age=[5, 6, np.NaN],
... born=[pd.NaT, pd.Timestamp('1939-05-27'),
... pd.Timestamp('1940-04-25')],
... name=['Alfred', 'Batman', ''],
... toy=[None, 'Batmobile', 'Joker']))
... born=[pd.NaT, pd.Timestamp('1939-05-27'),
... pd.Timestamp('1940-04-25')],
... name=['Alfred', 'Batman', ''],
... toy=[None, 'Batmobile', 'Joker']))
>>> df
age born name toy
0 5.0 NaT Alfred None
Expand Down Expand Up @@ -7761,10 +7761,10 @@ def notna(self: NDFrameT) -> NDFrameT:
Show which entries in a DataFrame are not NA.

>>> df = pd.DataFrame(dict(age=[5, 6, np.NaN],
... born=[pd.NaT, pd.Timestamp('1939-05-27'),
... pd.Timestamp('1940-04-25')],
... name=['Alfred', 'Batman', ''],
... toy=[None, 'Batmobile', 'Joker']))
... born=[pd.NaT, pd.Timestamp('1939-05-27'),
... pd.Timestamp('1940-04-25')],
... name=['Alfred', 'Batman', ''],
... toy=[None, 'Batmobile', 'Joker']))
>>> df
age born name toy
0 5.0 NaT Alfred None
Expand Down Expand Up @@ -8153,6 +8153,7 @@ def at_time(
Parameters
----------
time : datetime.time or str
The values to select.
axis : {0 or 'index', 1 or 'columns'}, default 0
For `Series` this parameter is unused and defaults to 0.

Expand Down Expand Up @@ -10109,7 +10110,8 @@ def tz_convert(
tz : str or tzinfo object or None
Target time zone. Passing ``None`` will convert to
UTC and remove the timezone information.
axis : the axis to convert
axis : {{0 or 'index', 1 or 'columns'}}, default 0
The axis to convert
level : int, str, default None
If axis is a MultiIndex, convert a specific level. Otherwise
must be None.
Expand All @@ -10130,8 +10132,10 @@ def tz_convert(
--------
Change to another time zone:

>>> s = pd.Series([1],
... index=pd.DatetimeIndex(['2018-09-15 01:30:00+02:00']))
>>> s = pd.Series(
... [1],
... index=pd.DatetimeIndex(['2018-09-15 01:30:00+02:00']),
... )
>>> s.tz_convert('Asia/Shanghai')
2018-09-15 07:30:00+08:00 1
dtype: int64
Expand Down Expand Up @@ -10196,7 +10200,8 @@ def tz_localize(
tz : str or tzinfo or None
Time zone to localize. Passing ``None`` will remove the
time zone information and preserve local time.
axis : the axis to localize
axis : {{0 or 'index', 1 or 'columns'}}, default 0
The axis to localize
level : int, str, default None
If axis ia a MultiIndex, localize a specific level. Otherwise
must be None.
Expand Down Expand Up @@ -10245,8 +10250,10 @@ def tz_localize(
--------
Localize local times:

>>> s = pd.Series([1],
... index=pd.DatetimeIndex(['2018-09-15 01:30:00']))
>>> s = pd.Series(
... [1],
... index=pd.DatetimeIndex(['2018-09-15 01:30:00']),
... )
>>> s.tz_localize('CET')
2018-09-15 01:30:00+02:00 1
dtype: int64
Expand Down Expand Up @@ -10480,9 +10487,9 @@ def describe(
Describing a timestamp ``Series``.

>>> s = pd.Series([
... np.datetime64("2000-01-01"),
... np.datetime64("2010-01-01"),
... np.datetime64("2010-01-01")
... np.datetime64("2000-01-01"),
... np.datetime64("2010-01-01"),
... np.datetime64("2010-01-01")
... ])
>>> s.describe()
count 3
Expand Down Expand Up @@ -11740,9 +11747,9 @@ def _doc_params(cls):
Examples
--------
>>> df = pd.DataFrame({'person_id': [0, 1, 2, 3],
... 'age': [21, 25, 62, 43],
... 'height': [1.61, 1.87, 1.49, 2.01]}
... ).set_index('person_id')
... 'age': [21, 25, 62, 43],
... 'height': [1.61, 1.87, 1.49, 2.01]}
... ).set_index('person_id')
>>> df
age height
person_id
Expand Down Expand Up @@ -11960,7 +11967,7 @@ def _doc_params(cls):
>>> df = pd.DataFrame([[2.0, 1.0],
... [3.0, np.nan],
... [1.0, 0.0]],
... columns=list('AB'))
... columns=list('AB'))
>>> df
A B
0 2.0 1.0
Expand Down Expand Up @@ -12025,7 +12032,7 @@ def _doc_params(cls):
>>> df = pd.DataFrame([[2.0, 1.0],
... [3.0, np.nan],
... [1.0, 0.0]],
... columns=list('AB'))
... columns=list('AB'))
>>> df
A B
0 2.0 1.0
Expand Down Expand Up @@ -12090,7 +12097,7 @@ def _doc_params(cls):
>>> df = pd.DataFrame([[2.0, 1.0],
... [3.0, np.nan],
... [1.0, 0.0]],
... columns=list('AB'))
... columns=list('AB'))
>>> df
A B
0 2.0 1.0
Expand Down Expand Up @@ -12155,7 +12162,7 @@ def _doc_params(cls):
>>> df = pd.DataFrame([[2.0, 1.0],
... [3.0, np.nan],
... [1.0, 0.0]],
... columns=list('AB'))
... columns=list('AB'))
>>> df
A B
0 2.0 1.0
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/shared_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -798,8 +798,8 @@
Consider a dataset containing food consumption in Argentina.

>>> df = pd.DataFrame({{'consumption': [10.51, 103.11, 55.48],
... 'co2_emissions': [37.2, 19.66, 1712]}},
... index=['Pork', 'Wheat Products', 'Beef'])
... 'co2_emissions': [37.2, 19.66, 1712]}},
... index=['Pork', 'Wheat Products', 'Beef'])

>>> df
consumption co2_emissions
Expand Down Expand Up @@ -865,8 +865,8 @@
Consider a dataset containing food consumption in Argentina.

>>> df = pd.DataFrame({{'consumption': [10.51, 103.11, 55.48],
... 'co2_emissions': [37.2, 19.66, 1712]}},
... index=['Pork', 'Wheat Products', 'Beef'])
... 'co2_emissions': [37.2, 19.66, 1712]}},
... index=['Pork', 'Wheat Products', 'Beef'])

>>> df
consumption co2_emissions
Expand Down
8 changes: 4 additions & 4 deletions pandas/core/window/doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ def create_section_header(header: str) -> str:

template_see_also = dedent(
"""
pandas.Series.{window_method} : Calling {window_method} with Series data.
pandas.DataFrame.{window_method} : Calling {window_method} with DataFrames.
pandas.Series.{agg_method} : Aggregating {agg_method} for Series.
pandas.DataFrame.{agg_method} : Aggregating {agg_method} for DataFrame.\n
Series.{window_method} : Calling {window_method} with Series data.
DataFrame.{window_method} : Calling {window_method} with DataFrames.
Series.{agg_method} : Aggregating {agg_method} for Series.
DataFrame.{agg_method} : Aggregating {agg_method} for DataFrame.\n
"""
).replace("\n", "", 1)

Expand Down