Skip to content

Commit 44e0e5d

Browse files
committed
Use mapper named argument
1 parent fa4358d commit 44e0e5d

File tree

2 files changed

+33
-24
lines changed

2 files changed

+33
-24
lines changed

pandas/core/frame.py

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
is_datetimetz,
4242
is_datetime64_any_dtype,
4343
is_datetime64tz_dtype,
44+
is_dict_like,
4445
is_bool_dtype,
4546
is_integer_dtype,
4647
is_float_dtype,
@@ -2904,27 +2905,30 @@ def reindex_axis(self, labels, axis=0, method=None, level=None, copy=True,
29042905
limit=limit, fill_value=fill_value)
29052906

29062907
@Appender(_shared_docs['rename'] % _shared_doc_kwargs)
2907-
def rename(self, *args, index=None, columns=None, **kwargs):
2908-
nargs = len(args)
2909-
if 'axis' in kwargs and (index is not None or columns is not None):
2910-
raise TypeError("Cannot specify 'index' or 'columns' and 'axis' at"
2911-
" the same time. Specify either\n"
2912-
"\t.rename(mapping, axis=axis), or\n"
2913-
"\t.rename(index=index, columns=columns)")
2914-
if 'axis' in kwargs and nargs > 1:
2915-
raise TypeError("Cannot specify 'index', 'axis', and 'columns' at "
2916-
"the same time.")
2917-
if nargs > 2:
2918-
raise TypeError("Too many positional arguments")
2919-
2920-
if nargs and index is not None:
2921-
raise TypeError("rename() got multiple arguments for argumnet "
2922-
"'index'")
2923-
2924-
if index is None and nargs:
2925-
index = args[0]
2926-
if columns is None and nargs > 1:
2927-
columns = args[1]
2908+
def rename(self, mapper=None, index=None, columns=None, axis=None,
2909+
**kwargs):
2910+
if axis is not None:
2911+
axis = self._get_axis_name(axis)
2912+
# interpreting 'mapper' as a positional argument
2913+
if index is not None or columns is not None:
2914+
raise TypeError("Can't specify 'index' or 'columns' and 'axis'"
2915+
" at the same time. Specify either\n"
2916+
"\t.rename(mapper, axis=axis), or\n"
2917+
"\t.rename(index=index, columns=columns)")
2918+
if axis == 'index':
2919+
index = mapper
2920+
elif axis == 'columns':
2921+
columns = mapper
2922+
elif all(x is not None for x in (mapper, index, columns)):
2923+
raise TypeError("Cannot specify all of 'mapper', 'index', and "
2924+
"'columns'. Specify 'mapper' and 'axis', or "
2925+
"'index' and 'columns'.")
2926+
elif axis is None and (mapper is not None and index is not None):
2927+
# Determine if they meant df.rename(idx_map, col_map)
2928+
if callable(index) or is_dict_like(index):
2929+
warnings.warn("Interpreting renaming index and columns. "
2930+
"Use keyword arguments.", UserWarning)
2931+
index, columns = mapper, index
29282932

29292933
return super(DataFrame, self).rename(index=index, columns=columns,
29302934
**kwargs)

pandas/tests/frame/test_alter_axes.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -894,9 +894,6 @@ def test_rename_raises(self):
894894
with tm.assert_raises_regex(TypeError, None):
895895
df.rename(str.lower, str.lower, str.lower)
896896

897-
with tm.assert_raises_regex(TypeError, None):
898-
df.rename(str.lower, index=str.lower)
899-
900897
def test_assign_columns(self):
901898
self.frame['hi'] = 'there'
902899

@@ -920,6 +917,14 @@ def test_set_index_preserve_categorical_dtype(self):
920917
result = result.reindex(columns=df.columns)
921918
tm.assert_frame_equal(result, df)
922919

920+
def test_ambiguous_warns(self):
921+
df = pd.DataFrame({"A": [1, 2]})
922+
with tm.assert_produces_warning(UserWarning):
923+
df.rename(id, id)
924+
925+
with tm.assert_produces_warning(UserWarning):
926+
df.rename({0: 10}, {"A": "B"})
927+
923928

924929
class TestIntervalIndex(object):
925930

0 commit comments

Comments
 (0)