Skip to content

Commit 7882aac

Browse files
MarcoGorelliWillAyd
authored andcommitted
📝 set klass correctly for series and dataframe set_axis (#30885)
1 parent 3fbc332 commit 7882aac

File tree

3 files changed

+71
-49
lines changed

3 files changed

+71
-49
lines changed

pandas/core/frame.py

+40
Original file line numberDiff line numberDiff line change
@@ -3824,6 +3824,46 @@ def align(
38243824
broadcast_axis=broadcast_axis,
38253825
)
38263826

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

pandas/core/generic.py

+5-49
Original file line numberDiff line numberDiff line change
@@ -559,7 +559,7 @@ def set_axis(self, labels, axis=0, inplace=False):
559559
"""
560560
Assign desired index to given axis.
561561
562-
Indexes for column or row labels can be changed by assigning
562+
Indexes for%(extended_summary_sub)s row labels can be changed by assigning
563563
a list-like or Index.
564564
565565
.. versionchanged:: 0.21.0
@@ -574,67 +574,23 @@ def set_axis(self, labels, axis=0, inplace=False):
574574
labels : list-like, Index
575575
The values for the new index.
576576
577-
axis : {0 or 'index', 1 or 'columns'}, default 0
578-
The axis to update. The value 0 identifies the rows, and 1
579-
identifies the columns.
577+
axis : %(axes_single_arg)s, default 0
578+
The axis to update. The value 0 identifies the rows%(axis_description_sub)s.
580579
581580
inplace : bool, default False
582581
Whether to return a new %(klass)s instance.
583582
584583
Returns
585584
-------
586585
renamed : %(klass)s or None
587-
An object of same type as caller if inplace=False, None otherwise.
586+
An object of type %(klass)s if inplace=False, None otherwise.
588587
589588
See Also
590589
--------
591-
DataFrame.rename_axis : Alter the name of the index or columns.
590+
%(klass)s.rename_axis : Alter the name of the index%(see_also_sub)s.
592591
593592
Examples
594593
--------
595-
**Series**
596-
597-
>>> s = pd.Series([1, 2, 3])
598-
>>> s
599-
0 1
600-
1 2
601-
2 3
602-
dtype: int64
603-
604-
>>> s.set_axis(['a', 'b', 'c'], axis=0)
605-
a 1
606-
b 2
607-
c 3
608-
dtype: int64
609-
610-
**DataFrame**
611-
612-
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
613-
614-
Change the row labels.
615-
616-
>>> df.set_axis(['a', 'b', 'c'], axis='index')
617-
A B
618-
a 1 4
619-
b 2 5
620-
c 3 6
621-
622-
Change the column labels.
623-
624-
>>> df.set_axis(['I', 'II'], axis='columns')
625-
I II
626-
0 1 4
627-
1 2 5
628-
2 3 6
629-
630-
Now, update the labels inplace.
631-
632-
>>> df.set_axis(['i', 'ii'], axis='columns', inplace=True)
633-
>>> df
634-
i ii
635-
0 1 4
636-
1 2 5
637-
2 3 6
638594
"""
639595
if inplace:
640596
setattr(self, self._get_axis_name(axis), labels)

pandas/core/series.py

+26
Original file line numberDiff line numberDiff line change
@@ -3948,6 +3948,32 @@ def rename(
39483948
else:
39493949
return self._set_name(index, inplace=inplace)
39503950

3951+
@Appender(
3952+
"""
3953+
>>> s = pd.Series([1, 2, 3])
3954+
>>> s
3955+
0 1
3956+
1 2
3957+
2 3
3958+
dtype: int64
3959+
3960+
>>> s.set_axis(['a', 'b', 'c'], axis=0)
3961+
a 1
3962+
b 2
3963+
c 3
3964+
dtype: int64
3965+
"""
3966+
)
3967+
@Substitution(
3968+
**_shared_doc_kwargs,
3969+
extended_summary_sub="",
3970+
axis_description_sub="",
3971+
see_also_sub="",
3972+
)
3973+
@Appender(generic.NDFrame.set_axis.__doc__)
3974+
def set_axis(self, labels, axis=0, inplace=False):
3975+
return super().set_axis(labels, axis=axis, inplace=inplace)
3976+
39513977
@Substitution(**_shared_doc_kwargs)
39523978
@Appender(generic.NDFrame.reindex.__doc__)
39533979
def reindex(self, index=None, **kwargs):

0 commit comments

Comments
 (0)