Skip to content

[DOC] set klass correctly for series and dataframe set_axis #30885

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 1 commit into from
Jan 20, 2020
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
40 changes: 40 additions & 0 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3819,6 +3819,46 @@ def align(
broadcast_axis=broadcast_axis,
)

@Appender(
"""
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})

Change the row labels.

>>> df.set_axis(['a', 'b', 'c'], axis='index')
A B
a 1 4
b 2 5
c 3 6

Change the column labels.

>>> df.set_axis(['I', 'II'], axis='columns')
I II
0 1 4
1 2 5
2 3 6

Now, update the labels inplace.

>>> df.set_axis(['i', 'ii'], axis='columns', inplace=True)
>>> df
i ii
0 1 4
1 2 5
2 3 6
"""
)
@Substitution(
**_shared_doc_kwargs,
extended_summary_sub=" column or",
axis_description_sub=", and 1 identifies the columns",
see_also_sub=" or columns",
)
@Appender(NDFrame.set_axis.__doc__)
def set_axis(self, labels, axis=0, inplace=False):
return super().set_axis(labels, axis=axis, inplace=inplace)

@Substitution(**_shared_doc_kwargs)
@Appender(NDFrame.reindex.__doc__)
@rewrite_axis_style_signature(
Expand Down
54 changes: 5 additions & 49 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,7 @@ def set_axis(self, labels, axis=0, inplace=False):
"""
Assign desired index to given axis.

Indexes for column or row labels can be changed by assigning
Indexes for%(extended_summary_sub)s row labels can be changed by assigning
a list-like or Index.

.. versionchanged:: 0.21.0
Expand All @@ -588,67 +588,23 @@ def set_axis(self, labels, axis=0, inplace=False):
labels : list-like, Index
The values for the new index.

axis : {0 or 'index', 1 or 'columns'}, default 0
The axis to update. The value 0 identifies the rows, and 1
identifies the columns.
axis : %(axes_single_arg)s, default 0
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually being substituted?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like it, see below

The axis to update. The value 0 identifies the rows%(axis_description_sub)s.

inplace : bool, default False
Whether to return a new %(klass)s instance.

Returns
-------
renamed : %(klass)s or None
An object of same type as caller if inplace=False, None otherwise.
An object of type %(klass)s if inplace=False, None otherwise.

See Also
--------
DataFrame.rename_axis : Alter the name of the index or columns.
%(klass)s.rename_axis : Alter the name of the index%(see_also_sub)s.

Examples
--------
**Series**

>>> s = pd.Series([1, 2, 3])
>>> s
0 1
1 2
2 3
dtype: int64

>>> s.set_axis(['a', 'b', 'c'], axis=0)
a 1
b 2
c 3
dtype: int64

**DataFrame**

>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})

Change the row labels.

>>> df.set_axis(['a', 'b', 'c'], axis='index')
A B
a 1 4
b 2 5
c 3 6

Change the column labels.

>>> df.set_axis(['I', 'II'], axis='columns')
I II
0 1 4
1 2 5
2 3 6

Now, update the labels inplace.

>>> df.set_axis(['i', 'ii'], axis='columns', inplace=True)
>>> df
i ii
0 1 4
1 2 5
2 3 6
"""
if inplace:
setattr(self, self._get_axis_name(axis), labels)
Expand Down
26 changes: 26 additions & 0 deletions pandas/core/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -3986,6 +3986,32 @@ def rename(
else:
return self._set_name(index, inplace=inplace)

@Appender(
"""
>>> s = pd.Series([1, 2, 3])
>>> s
0 1
1 2
2 3
dtype: int64

>>> s.set_axis(['a', 'b', 'c'], axis=0)
a 1
b 2
c 3
dtype: int64
"""
)
@Substitution(
**_shared_doc_kwargs,
extended_summary_sub="",
axis_description_sub="",
see_also_sub="",
)
@Appender(generic.NDFrame.set_axis.__doc__)
def set_axis(self, labels, axis=0, inplace=False):
return super().set_axis(labels, axis=axis, inplace=inplace)

@Substitution(**_shared_doc_kwargs)
@Appender(generic.NDFrame.reindex.__doc__)
def reindex(self, index=None, **kwargs):
Expand Down