Skip to content

Commit e8e0aae

Browse files
gliptakjorisvandenbossche
authored andcommitted
DOC: Update documentation for rename (pandas-dev#13533)
1 parent 3944a36 commit e8e0aae

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

doc/source/basics.rst

+5-2
Original file line numberDiff line numberDiff line change
@@ -1159,14 +1159,17 @@ mapping (a dict or Series) or an arbitrary function.
11591159
s.rename(str.upper)
11601160
11611161
If you pass a function, it must return a value when called with any of the
1162-
labels (and must produce a set of unique values). But if you pass a dict or
1163-
Series, it need only contain a subset of the labels as keys:
1162+
labels (and must produce a set of unique values). A dict or
1163+
Series can also be used:
11641164

11651165
.. ipython:: python
11661166
11671167
df.rename(columns={'one' : 'foo', 'two' : 'bar'},
11681168
index={'a' : 'apple', 'b' : 'banana', 'd' : 'durian'})
11691169
1170+
If the mapping doesn't include a column/index label, it isn't renamed. Also
1171+
extra labels in the mapping don't throw an error.
1172+
11701173
The :meth:`~DataFrame.rename` method also provides an ``inplace`` named
11711174
parameter that is by default ``False`` and copies the underlying data. Pass
11721175
``inplace=True`` to rename the data in place.

pandas/core/generic.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -555,8 +555,8 @@ def swaplevel(self, i=-2, j=-1, axis=0):
555555
_shared_docs['rename'] = """
556556
Alter axes input function or functions. Function / dict values must be
557557
unique (1-to-1). Labels not contained in a dict / Series will be left
558-
as-is. Alternatively, change ``Series.name`` with a scalar
559-
value (Series only).
558+
as-is. Extra labels listed don't throw an error. Alternatively, change
559+
``Series.name`` with a scalar value (Series only).
560560
561561
Parameters
562562
----------
@@ -611,6 +611,11 @@ def swaplevel(self, i=-2, j=-1, axis=0):
611611
0 1 4
612612
1 2 5
613613
2 3 6
614+
>>> df.rename(index=str, columns={"A": "a", "C": "c"})
615+
a B
616+
0 1 4
617+
1 2 5
618+
2 3 6
614619
"""
615620

616621
@Appender(_shared_docs['rename'] % dict(axes='axes keywords for this'

0 commit comments

Comments
 (0)