Skip to content

Commit 749f456

Browse files
KatemaNLmroeschke
authored andcommitted
DOC removed unnecessary use of parameter in example rename function call (#26250)
* DOC removed unnecessary use of parameter in example rename function call removed unnecessary (and misleading) use of index parameter in docstring example function call of rename function * DOC added examples as discussed in the pull request review added an example for using index parameter only added an example for casting the index type * DOC fixed line to long * DOC fixed doctest fail because of unexpected linebreak * DOC simplified examples * DOC fixed the examples matching the output mentioned in the doc * DOC terminology change
1 parent 07cea19 commit 749f456

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

pandas/core/frame.py

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3995,20 +3995,28 @@ def rename(self, *args, **kwargs):
39953995
We *highly* recommend using keyword arguments to clarify your
39963996
intent.
39973997
3998+
Rename columns using a mapping:
39983999
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
3999-
>>> df.rename(index=str, columns={"A": "a", "B": "c"})
4000+
>>> df.rename(columns={"A": "a", "B": "c"})
40004001
a c
40014002
0 1 4
40024003
1 2 5
40034004
2 3 6
40044005
4005-
>>> df.rename(index=str, columns={"A": "a", "C": "c"})
4006-
a B
4007-
0 1 4
4008-
1 2 5
4009-
2 3 6
4006+
Rename index using a mapping:
4007+
>>> df.rename(index={0: "x", 1: "y", 2: "z"})
4008+
A B
4009+
x 1 4
4010+
y 2 5
4011+
z 3 6
4012+
4013+
Cast index labels to a different type:
4014+
>>> df.index
4015+
RangeIndex(start=0, stop=3, step=1)
4016+
>>> df.rename(index=str).index
4017+
Index(['0', '1', '2'], dtype='object')
40104018
4011-
>>> df.rename(index=str, columns={"A": "a", "C": "c"}, errors="raise")
4019+
>>> df.rename(columns={"A": "a", "B": "b", "C": "c"}, errors="raise")
40124020
Traceback (most recent call last):
40134021
KeyError: ['C'] not found in axis
40144022

0 commit comments

Comments
 (0)