@@ -524,24 +524,37 @@ def _expand_axes(self, key):
524
524
525
525
return new_axes
526
526
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.
528
540
529
541
Parameters
530
542
----------
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
+
534
550
inplace : boolean, default None
535
551
Whether to return a new %(klass)s instance.
536
552
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::
540
554
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.
545
558
546
559
Returns
547
560
-------
@@ -550,43 +563,62 @@ def _expand_axes(self, key):
550
563
551
564
See Also
552
565
--------
553
- pandas.NDFrame.rename
566
+ pandas.DataFrame.rename_axis : Alter the name of the index or columns.
554
567
555
568
Examples
556
569
--------
570
+ **Series**
571
+
557
572
>>> s = pd.Series([1, 2, 3])
558
573
>>> s
559
574
0 1
560
575
1 2
561
576
2 3
562
577
dtype: int64
578
+
563
579
>>> s.set_axis(['a', 'b', 'c'], axis=0, inplace=False)
564
580
a 1
565
581
b 2
566
582
c 3
567
583
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
+
568
595
>>> 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)
570
600
A B
571
601
a 1 4
572
602
b 2 5
573
603
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)
575
608
I II
576
609
0 1 4
577
610
1 2 5
578
611
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)
580
616
>>> df
581
617
i ii
582
618
0 1 4
583
619
1 2 5
584
620
2 3 6
585
-
586
621
"""
587
-
588
- @Appender (_shared_docs ['set_axis' ] % dict (klass = 'NDFrame' ))
589
- def set_axis (self , labels , axis = 0 , inplace = None ):
590
622
if is_scalar (labels ):
591
623
warnings .warn (
592
624
'set_axis now takes "labels" as first argument, and '
0 commit comments