Skip to content

Commit 1e62a0e

Browse files
committed
Refactor for reindex
1 parent 7ac2e3b commit 1e62a0e

File tree

1 file changed

+43
-29
lines changed

1 file changed

+43
-29
lines changed

pandas/core/frame.py

+43-29
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
is_datetimetz,
4242
is_datetime64_any_dtype,
4343
is_datetime64tz_dtype,
44-
is_dict_like,
4544
is_bool_dtype,
4645
is_integer_dtype,
4746
is_float_dtype,
@@ -2769,6 +2768,46 @@ def reindexer(value):
27692768

27702769
return np.atleast_2d(np.asarray(value))
27712770

2771+
def _validate_axis_style_args(self, arg, arg_name, index, columns,
2772+
axis, method_name):
2773+
if axis is not None:
2774+
# Using "axis" style, along with a positional arg
2775+
# Both index and columns should be None then
2776+
axis = self._get_axis_name(axis)
2777+
if index is not None or columns is not None:
2778+
msg = (
2779+
"Can't specify both 'axis' and 'index' or 'columns'. "
2780+
"Specify either\n"
2781+
"\t.{method_name}.rename({arg_name}, axis=axis), or\n"
2782+
"\t.{method_name}.rename(index=index, columns=columns)"
2783+
).format(arg_name=arg_name, method_name=method_name)
2784+
raise TypeError(msg)
2785+
if axis == 'index':
2786+
index = arg
2787+
elif axis == 'columns':
2788+
columns = arg
2789+
2790+
elif all(x is not None for x in (arg, index, columns)):
2791+
msg = (
2792+
"Cannot specify all of '{arg_name}', 'index', and 'columns'. "
2793+
"Specify either {arg_name} and 'axis', or 'index' and "
2794+
"'columns'."
2795+
).format(arg_name=arg_name)
2796+
raise TypeError(msg)
2797+
2798+
elif axis is None and (arg is not None and index is not None):
2799+
# This is the "ambiguous" case, so emit a warning
2800+
msg = (
2801+
"Interpreting call to '.{method_name}(a, b)' as "
2802+
"'.{method_name}(index=a, columns=b)'. "
2803+
"Use keyword arguments to remove any ambiguity."
2804+
).format(method_name=method_name)
2805+
warnings.warn(msg)
2806+
index, columns = arg, index
2807+
elif index is None and columns is None:
2808+
index = arg
2809+
return index, columns
2810+
27722811
@property
27732812
def _series(self):
27742813
result = {}
@@ -2910,34 +2949,9 @@ def reindex_axis(self, labels, axis=0, method=None, level=None, copy=True,
29102949
@Appender(_shared_docs['rename'] % _shared_doc_kwargs)
29112950
def rename(self, mapper=None, index=None, columns=None, axis=None,
29122951
**kwargs):
2913-
if axis is not None:
2914-
# Using "axis" style, along with a positional mapper
2915-
# Both index and columns should be None then
2916-
axis = self._get_axis_name(axis)
2917-
if index is not None or columns is not None:
2918-
raise TypeError("Can't specify both 'axis' and 'index' or "
2919-
"'columns' Specify either\n"
2920-
"\t.rename(mapper, axis=axis), or\n"
2921-
"\t.rename(index=index, columns=columns)")
2922-
if axis == 'index':
2923-
index = mapper
2924-
elif axis == 'columns':
2925-
columns = mapper
2926-
elif all(x is not None for x in (mapper, index, columns)):
2927-
raise TypeError("Cannot specify all of 'mapper', 'index', and "
2928-
"'columns'. Specify 'mapper' and 'axis', or "
2929-
"'index' and 'columns'.")
2930-
elif axis is None and (mapper is not None and index is not None):
2931-
# Determine if they meant df.rename(idx_map, col_map)
2932-
if callable(index) or is_dict_like(index):
2933-
warnings.warn("Interpreting call to '.rename(a, b)' as "
2934-
"'.rename(index=a, columns=b)'. "
2935-
"Use keyword arguments to remove ambiguity.",
2936-
UserWarning)
2937-
index, columns = mapper, index
2938-
elif index is None and columns is None:
2939-
index = mapper
2940-
2952+
index, columns = self._validate_axis_style_args(mapper, 'mapper',
2953+
index, columns,
2954+
axis, 'rename')
29412955
return super(DataFrame, self).rename(index=index, columns=columns,
29422956
**kwargs)
29432957

0 commit comments

Comments
 (0)