@@ -6570,16 +6570,17 @@ def __array__(self, dtype=None):
6570
6570
FO 2 3
6571
6571
"""
6572
6572
def set_labels (self , axis = None , labels = None , inplace = False , ** kwargs ):
6573
- """Replaces the labels of an axis of array.
6573
+ r """Replaces the labels of an axis of array.
6574
6574
6575
6575
Parameters
6576
6576
----------
6577
6577
axis : string or Axis or dict
6578
6578
Axis for which we want to replace labels, or mapping {axis: changes} where changes can either be the
6579
- complete list of labels or a mapping {old_label: new_label}.
6580
- labels : int, str, iterable or mapping, optional
6579
+ complete list of labels, a mapping {old_label: new_label} or a function to transform labels .
6580
+ labels : int, str, iterable or mapping or function , optional
6581
6581
Integer or list of values usable as the collection of labels for an Axis. If this is mapping, it must be
6582
- {old_label: new_label}. This argument must not be used if axis is a mapping.
6582
+ {old_label: new_label}. If it is a function, it must be a function accepting a single argument (a
6583
+ label) and returning a single value. This argument must not be used if axis is a mapping.
6583
6584
inplace : bool, optional
6584
6585
Whether or not to modify the original object or return a new array and leave the original intact.
6585
6586
Defaults to False.
@@ -6595,48 +6596,55 @@ def set_labels(self, axis=None, labels=None, inplace=False, **kwargs):
6595
6596
--------
6596
6597
>>> a = ndtest('nat=BE,FO;sex=M,F')
6597
6598
>>> a
6598
- nat\\ sex M F
6599
+ nat\sex M F
6599
6600
BE 0 1
6600
6601
FO 2 3
6601
- >>> a.set_labels(X. sex, ['Men', 'Women'])
6602
- nat\\ sex Men Women
6602
+ >>> a.set_labels(' sex' , ['Men', 'Women'])
6603
+ nat\sex Men Women
6603
6604
BE 0 1
6604
6605
FO 2 3
6605
6606
6606
6607
when passing a single string as labels, it will be interpreted to create the list of labels, so that one can
6607
6608
use the same syntax than during axis creation.
6608
6609
6609
- >>> a.set_labels(X. sex, 'Men,Women')
6610
- nat\\ sex Men Women
6610
+ >>> a.set_labels(' sex' , 'Men,Women')
6611
+ nat\sex Men Women
6611
6612
BE 0 1
6612
6613
FO 2 3
6613
6614
6614
6615
to replace only some labels, one must give a mapping giving the new label for each label to replace
6615
6616
6616
- >>> a.set_labels(X. sex, {'M': 'Men'})
6617
- nat\\ sex Men F
6617
+ >>> a.set_labels(' sex' , {'M': 'Men'})
6618
+ nat\sex Men F
6618
6619
BE 0 1
6619
6620
FO 2 3
6620
6621
6622
+ to transform labels by a function, use any function accepting and returning a single argument:
6623
+
6624
+ >>> a.set_labels('nat', str.lower)
6625
+ nat\sex M F
6626
+ be 0 1
6627
+ fo 2 3
6628
+
6621
6629
to replace labels for several axes at the same time, one should give a mapping giving the new labels for each
6622
6630
changed axis
6623
6631
6624
6632
>>> a.set_labels({'sex': 'Men,Women', 'nat': 'Belgian,Foreigner'})
6625
- nat\\ sex Men Women
6633
+ nat\sex Men Women
6626
6634
Belgian 0 1
6627
6635
Foreigner 2 3
6628
6636
6629
6637
or use keyword arguments
6630
6638
6631
6639
>>> a.set_labels(sex='Men,Women', nat='Belgian,Foreigner')
6632
- nat\\ sex Men Women
6640
+ nat\sex Men Women
6633
6641
Belgian 0 1
6634
6642
Foreigner 2 3
6635
6643
6636
6644
one can also replace some labels in several axes by giving a mapping of mappings
6637
6645
6638
6646
>>> a.set_labels({'sex': {'M': 'Men'}, 'nat': {'BE': 'Belgian'}})
6639
- nat\\ sex Men F
6647
+ nat\sex Men F
6640
6648
Belgian 0 1
6641
6649
FO 2 3
6642
6650
"""
@@ -6656,6 +6664,8 @@ def set_labels(self, axis=None, labels=None, inplace=False, **kwargs):
6656
6664
real_axis = self .axes [old_axis ]
6657
6665
if isinstance (axis_changes , dict ):
6658
6666
new_axis = real_axis .replace (axis_changes )
6667
+ elif callable (axis_changes ):
6668
+ new_axis = real_axis .apply (axis_changes )
6659
6669
else :
6660
6670
new_axis = Axis (axis_changes , real_axis .name )
6661
6671
new_axes .append ((old_axis , new_axis ))
0 commit comments