Skip to content

Commit 2478296

Browse files
committed
Rework, docstring
1 parent 44e0e5d commit 2478296

File tree

6 files changed

+37
-5
lines changed

6 files changed

+37
-5
lines changed

doc/source/whatsnew/v0.21.0.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,10 @@ alternative to specify the ``axis`` to target (:issue:`12392`).
119119

120120
df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
121121
df.rename(str.lower, axis='columns')
122+
df.rename(id, axis='index')
122123

123-
The ``.rename(columns=str.lower)`` style continues to work as before.
124+
The ``.rename(index=id, columns=str.lower)`` style continues to work as before.
125+
We *highly* encourage using named arguments to avoid confusion.
124126

125127
.. _whatsnew_0210.enhancements.categorical_dtype:
126128

pandas/core/frame.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,10 @@
112112
optional_by="""
113113
by : str or list of str
114114
Name or list of names which refer to the axis items.""",
115-
versionadded_to_excel='')
115+
versionadded_to_excel='',
116+
mapper = """mapper : dict-like or function
117+
Applied to the axis specified by `axis`""",
118+
)
116119

117120
_numeric_only_doc = """numeric_only : boolean, default None
118121
Include only float, int, boolean data. If None, will attempt to use
@@ -2929,6 +2932,8 @@ def rename(self, mapper=None, index=None, columns=None, axis=None,
29292932
warnings.warn("Interpreting renaming index and columns. "
29302933
"Use keyword arguments.", UserWarning)
29312934
index, columns = mapper, index
2935+
elif index is None and columns is None:
2936+
index = mapper
29322937

29332938
return super(DataFrame, self).rename(index=index, columns=columns,
29342939
**kwargs)

pandas/core/generic.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -742,6 +742,7 @@ def swaplevel(self, i=-2, j=-1, axis=0):
742742
743743
Parameters
744744
----------
745+
%(mapper)s
745746
%(axes)s : scalar, list-like, dict-like or function, optional
746747
Scalar or list-like will alter the ``Series.name`` attribute,
747748
and raise on DataFrame or Panel.
@@ -787,6 +788,7 @@ def swaplevel(self, i=-2, j=-1, axis=0):
787788
3 2
788789
5 3
789790
dtype: int64
791+
790792
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
791793
>>> df.rename(2)
792794
Traceback (most recent call last):
@@ -802,10 +804,25 @@ def swaplevel(self, i=-2, j=-1, axis=0):
802804
0 1 4
803805
1 2 5
804806
2 3 6
807+
808+
Using axis-style parameters
809+
810+
>>> df.rename(str.lower, axis='columns')
811+
A B
812+
0 1 4
813+
1 2 5
814+
2 3 6
815+
816+
>>> df.rename({1: 2, 2: 4}, axis='index')
817+
A B
818+
0 1 4
819+
2 2 5
820+
4 3 6
805821
"""
806822

807823
@Appender(_shared_docs['rename'] % dict(axes='axes keywords for this'
808-
' object', klass='NDFrame'))
824+
' object', klass='NDFrame',
825+
mapper=''))
809826
def rename(self, *args, **kwargs):
810827
axes, kwargs = self._construct_axes_from_arguments(args, kwargs)
811828
copy = kwargs.pop('copy', True)

pandas/core/panel.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
_shared_doc_kwargs = dict(
4040
axes='items, major_axis, minor_axis',
4141
klass="Panel",
42-
axes_single_arg="{0, 1, 2, 'items', 'major_axis', 'minor_axis'}")
42+
axes_single_arg="{0, 1, 2, 'items', 'major_axis', 'minor_axis'}",
43+
mapper='')
4344
_shared_doc_kwargs['args_transpose'] = ("three positional arguments: each one"
4445
"of\n%s" %
4546
_shared_doc_kwargs['axes_single_arg'])

pandas/core/series.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@
8585
inplace="""inplace : boolean, default False
8686
If True, performs operation inplace and returns None.""",
8787
unique='np.ndarray', duplicated='Series',
88-
optional_by='',
88+
optional_by='', mapper='',
8989
versionadded_to_excel='\n .. versionadded:: 0.20.0\n')
9090

9191

pandas/tests/frame/test_alter_axes.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,13 @@ def test_rename_columns(self):
867867
result = df.rename({'X': 'x', 'Y': 'y'}, axis='index')
868868
assert_frame_equal(result, expected)
869869

870+
def test_rename_mapper_multi(self):
871+
df = pd.DataFrame({"A": ['a', 'b'], "B": ['c', 'd'],
872+
'C': [1, 2]}).set_index(["A", "B"])
873+
result = df.rename(str.upper)
874+
expected = df.rename(index=str.upper)
875+
assert_frame_equal(result, expected)
876+
870877
def test_rename_raises(self):
871878
df = pd.DataFrame({"A": [1, 2], "B": [1, 2]}, index=['0', '1'])
872879

0 commit comments

Comments
 (0)