Skip to content

Commit 160dbb3

Browse files
author
Marco Gorelli
committed
📝 set klass correctly for series and dataframe set_axis
1 parent 439d629 commit 160dbb3

File tree

3 files changed

+71
-49
lines changed

3 files changed

+71
-49
lines changed

pandas/core/frame.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3819,6 +3819,46 @@ def align(
38193819
broadcast_axis=broadcast_axis,
38203820
)
38213821

3822+
@Appender(
3823+
"""
3824+
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
3825+
3826+
Change the row labels.
3827+
3828+
>>> df.set_axis(['a', 'b', 'c'], axis='index')
3829+
A B
3830+
a 1 4
3831+
b 2 5
3832+
c 3 6
3833+
3834+
Change the column labels.
3835+
3836+
>>> df.set_axis(['I', 'II'], axis='columns')
3837+
I II
3838+
0 1 4
3839+
1 2 5
3840+
2 3 6
3841+
3842+
Now, update the labels inplace.
3843+
3844+
>>> df.set_axis(['i', 'ii'], axis='columns', inplace=True)
3845+
>>> df
3846+
i ii
3847+
0 1 4
3848+
1 2 5
3849+
2 3 6
3850+
"""
3851+
)
3852+
@Substitution(
3853+
**_shared_doc_kwargs,
3854+
extended_summary_sub=" column or",
3855+
axis_description_sub=", and 1 identifies the columns",
3856+
see_also_sub=" or columns",
3857+
)
3858+
@Appender(NDFrame.set_axis.__doc__)
3859+
def set_axis(self, labels, axis=0, inplace=False):
3860+
return super().set_axis(labels, axis=axis, inplace=inplace)
3861+
38223862
@Substitution(**_shared_doc_kwargs)
38233863
@Appender(NDFrame.reindex.__doc__)
38243864
@rewrite_axis_style_signature(

pandas/core/generic.py

Lines changed: 5 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -573,7 +573,7 @@ def set_axis(self, labels, axis=0, inplace=False):
573573
"""
574574
Assign desired index to given axis.
575575
576-
Indexes for column or row labels can be changed by assigning
576+
Indexes for%(extended_summary_sub)s row labels can be changed by assigning
577577
a list-like or Index.
578578
579579
.. versionchanged:: 0.21.0
@@ -588,67 +588,23 @@ def set_axis(self, labels, axis=0, inplace=False):
588588
labels : list-like, Index
589589
The values for the new index.
590590
591-
axis : {0 or 'index', 1 or 'columns'}, default 0
592-
The axis to update. The value 0 identifies the rows, and 1
593-
identifies the columns.
591+
axis : %(axes_single_arg)s, default 0
592+
The axis to update. The value 0 identifies the rows%(axis_description_sub)s.
594593
595594
inplace : bool, default False
596595
Whether to return a new %(klass)s instance.
597596
598597
Returns
599598
-------
600599
renamed : %(klass)s or None
601-
An object of same type as caller if inplace=False, None otherwise.
600+
An object of type %(klass)s if inplace=False, None otherwise.
602601
603602
See Also
604603
--------
605-
DataFrame.rename_axis : Alter the name of the index or columns.
604+
%(klass)s.rename_axis : Alter the name of the index%(see_also_sub)s.
606605
607606
Examples
608607
--------
609-
**Series**
610-
611-
>>> s = pd.Series([1, 2, 3])
612-
>>> s
613-
0 1
614-
1 2
615-
2 3
616-
dtype: int64
617-
618-
>>> s.set_axis(['a', 'b', 'c'], axis=0)
619-
a 1
620-
b 2
621-
c 3
622-
dtype: int64
623-
624-
**DataFrame**
625-
626-
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
627-
628-
Change the row labels.
629-
630-
>>> df.set_axis(['a', 'b', 'c'], axis='index')
631-
A B
632-
a 1 4
633-
b 2 5
634-
c 3 6
635-
636-
Change the column labels.
637-
638-
>>> df.set_axis(['I', 'II'], axis='columns')
639-
I II
640-
0 1 4
641-
1 2 5
642-
2 3 6
643-
644-
Now, update the labels inplace.
645-
646-
>>> df.set_axis(['i', 'ii'], axis='columns', inplace=True)
647-
>>> df
648-
i ii
649-
0 1 4
650-
1 2 5
651-
2 3 6
652608
"""
653609
if inplace:
654610
setattr(self, self._get_axis_name(axis), labels)

pandas/core/series.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3986,6 +3986,32 @@ def rename(
39863986
else:
39873987
return self._set_name(index, inplace=inplace)
39883988

3989+
@Appender(
3990+
"""
3991+
>>> s = pd.Series([1, 2, 3])
3992+
>>> s
3993+
0 1
3994+
1 2
3995+
2 3
3996+
dtype: int64
3997+
3998+
>>> s.set_axis(['a', 'b', 'c'], axis=0)
3999+
a 1
4000+
b 2
4001+
c 3
4002+
dtype: int64
4003+
"""
4004+
)
4005+
@Substitution(
4006+
**_shared_doc_kwargs,
4007+
extended_summary_sub="",
4008+
axis_description_sub="",
4009+
see_also_sub="",
4010+
)
4011+
@Appender(generic.NDFrame.set_axis.__doc__)
4012+
def set_axis(self, labels, axis=0, inplace=False):
4013+
return super().set_axis(labels, axis=axis, inplace=inplace)
4014+
39894015
@Substitution(**_shared_doc_kwargs)
39904016
@Appender(generic.NDFrame.reindex.__doc__)
39914017
def reindex(self, index=None, **kwargs):

0 commit comments

Comments
 (0)