Skip to content

Commit 34312d3

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

File tree

3 files changed

+58
-45
lines changed

3 files changed

+58
-45
lines changed

pandas/core/frame.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3819,6 +3819,41 @@ 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(**_shared_doc_kwargs)
3853+
@Appender(NDFrame.set_axis.__doc__)
3854+
def set_axis(self, labels, axis=0, inplace=False):
3855+
return super().set_axis(labels, axis=axis, inplace=inplace)
3856+
38223857
@Substitution(**_shared_doc_kwargs)
38233858
@Appender(NDFrame.reindex.__doc__)
38243859
@rewrite_axis_style_signature(

pandas/core/generic.py

Lines changed: 2 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -598,57 +598,14 @@ def set_axis(self, labels, axis=0, inplace=False):
598598
Returns
599599
-------
600600
renamed : %(klass)s or None
601-
An object of same type as caller if inplace=False, None otherwise.
601+
An object of type %(klass)s if inplace=False, None otherwise.
602602
603603
See Also
604604
--------
605-
DataFrame.rename_axis : Alter the name of the index or columns.
605+
%(klass)s.rename_axis : Alter the name of the index or columns.
606606
607607
Examples
608608
--------
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
652609
"""
653610
if inplace:
654611
setattr(self, self._get_axis_name(axis), labels)

pandas/core/series.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3986,6 +3986,27 @@ 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(**_shared_doc_kwargs)
4006+
@Appender(generic.NDFrame.set_axis.__doc__)
4007+
def set_axis(self, labels, axis=0, inplace=False):
4008+
return super().set_axis(labels, axis=axis, inplace=inplace)
4009+
39894010
@Substitution(**_shared_doc_kwargs)
39904011
@Appender(generic.NDFrame.reindex.__doc__)
39914012
def reindex(self, index=None, **kwargs):

0 commit comments

Comments
 (0)