Skip to content

Commit ea8ac51

Browse files
astrastefaniaTomAugspurger
authored andcommitted
DOC: update the pandas.Series.set_axis docstring (#20164)
1 parent 30d712f commit ea8ac51

File tree

1 file changed

+51
-19
lines changed

1 file changed

+51
-19
lines changed

pandas/core/generic.py

+51-19
Original file line numberDiff line numberDiff line change
@@ -524,24 +524,37 @@ def _expand_axes(self, key):
524524

525525
return new_axes
526526

527-
_shared_docs['set_axis'] = """Assign desired index to given axis
527+
def set_axis(self, labels, axis=0, inplace=None):
528+
"""
529+
Assign desired index to given axis.
530+
531+
Indexes for column or row labels can be changed by assigning
532+
a list-like or Index.
533+
534+
.. versionchanged:: 0.21.0
535+
536+
The signature is now `labels` and `axis`, consistent with
537+
the rest of pandas API. Previously, the `axis` and `labels`
538+
arguments were respectively the first and second positional
539+
arguments.
528540
529541
Parameters
530542
----------
531-
labels: list-like or Index
532-
The values for the new index
533-
axis : int or string, default 0
543+
labels : list-like, Index
544+
The values for the new index.
545+
546+
axis : {0 or 'index', 1 or 'columns'}, default 0
547+
The axis to update. The value 0 identifies the rows, and 1
548+
identifies the columns.
549+
534550
inplace : boolean, default None
535551
Whether to return a new %(klass)s instance.
536552
537-
WARNING: inplace=None currently falls back to to True, but
538-
in a future version, will default to False. Use inplace=True
539-
explicitly rather than relying on the default.
553+
.. warning::
540554
541-
.. versionadded:: 0.21.0
542-
The signature is make consistent to the rest of the API.
543-
Previously, the "axis" and "labels" arguments were respectively
544-
the first and second positional arguments.
555+
``inplace=None`` currently falls back to to True, but in a
556+
future version, will default to False. Use inplace=True
557+
explicitly rather than relying on the default.
545558
546559
Returns
547560
-------
@@ -550,43 +563,62 @@ def _expand_axes(self, key):
550563
551564
See Also
552565
--------
553-
pandas.NDFrame.rename
566+
pandas.DataFrame.rename_axis : Alter the name of the index or columns.
554567
555568
Examples
556569
--------
570+
**Series**
571+
557572
>>> s = pd.Series([1, 2, 3])
558573
>>> s
559574
0 1
560575
1 2
561576
2 3
562577
dtype: int64
578+
563579
>>> s.set_axis(['a', 'b', 'c'], axis=0, inplace=False)
564580
a 1
565581
b 2
566582
c 3
567583
dtype: int64
584+
585+
The original object is not modified.
586+
587+
>>> s
588+
0 1
589+
1 2
590+
2 3
591+
dtype: int64
592+
593+
**DataFrame**
594+
568595
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
569-
>>> df.set_axis(['a', 'b', 'c'], axis=0, inplace=False)
596+
597+
Change the row labels.
598+
599+
>>> df.set_axis(['a', 'b', 'c'], axis='index', inplace=False)
570600
A B
571601
a 1 4
572602
b 2 5
573603
c 3 6
574-
>>> df.set_axis(['I', 'II'], axis=1, inplace=False)
604+
605+
Change the column labels.
606+
607+
>>> df.set_axis(['I', 'II'], axis='columns', inplace=False)
575608
I II
576609
0 1 4
577610
1 2 5
578611
2 3 6
579-
>>> df.set_axis(['i', 'ii'], axis=1, inplace=True)
612+
613+
Now, update the labels inplace.
614+
615+
>>> df.set_axis(['i', 'ii'], axis='columns', inplace=True)
580616
>>> df
581617
i ii
582618
0 1 4
583619
1 2 5
584620
2 3 6
585-
586621
"""
587-
588-
@Appender(_shared_docs['set_axis'] % dict(klass='NDFrame'))
589-
def set_axis(self, labels, axis=0, inplace=None):
590622
if is_scalar(labels):
591623
warnings.warn(
592624
'set_axis now takes "labels" as first argument, and '

0 commit comments

Comments
 (0)