Skip to content

Commit

Permalink
Refactor for reindex
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAugspurger committed Oct 6, 2017
1 parent 7ac2e3b commit 1e62a0e
Showing 1 changed file with 43 additions and 29 deletions.
72 changes: 43 additions & 29 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
is_datetimetz,
is_datetime64_any_dtype,
is_datetime64tz_dtype,
is_dict_like,
is_bool_dtype,
is_integer_dtype,
is_float_dtype,
Expand Down Expand Up @@ -2769,6 +2768,46 @@ def reindexer(value):

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

def _validate_axis_style_args(self, arg, arg_name, index, columns,
axis, method_name):
if axis is not None:
# Using "axis" style, along with a positional arg
# Both index and columns should be None then
axis = self._get_axis_name(axis)
if index is not None or columns is not None:
msg = (
"Can't specify both 'axis' and 'index' or 'columns'. "
"Specify either\n"
"\t.{method_name}.rename({arg_name}, axis=axis), or\n"
"\t.{method_name}.rename(index=index, columns=columns)"
).format(arg_name=arg_name, method_name=method_name)
raise TypeError(msg)
if axis == 'index':
index = arg
elif axis == 'columns':
columns = arg

elif all(x is not None for x in (arg, index, columns)):
msg = (
"Cannot specify all of '{arg_name}', 'index', and 'columns'. "
"Specify either {arg_name} and 'axis', or 'index' and "
"'columns'."
).format(arg_name=arg_name)
raise TypeError(msg)

elif axis is None and (arg is not None and index is not None):
# This is the "ambiguous" case, so emit a warning
msg = (
"Interpreting call to '.{method_name}(a, b)' as "
"'.{method_name}(index=a, columns=b)'. "
"Use keyword arguments to remove any ambiguity."
).format(method_name=method_name)
warnings.warn(msg)
index, columns = arg, index
elif index is None and columns is None:
index = arg
return index, columns

@property
def _series(self):
result = {}
Expand Down Expand Up @@ -2910,34 +2949,9 @@ def reindex_axis(self, labels, axis=0, method=None, level=None, copy=True,
@Appender(_shared_docs['rename'] % _shared_doc_kwargs)
def rename(self, mapper=None, index=None, columns=None, axis=None,
**kwargs):
if axis is not None:
# Using "axis" style, along with a positional mapper
# Both index and columns should be None then
axis = self._get_axis_name(axis)
if index is not None or columns is not None:
raise TypeError("Can't specify both 'axis' and 'index' or "
"'columns' Specify either\n"
"\t.rename(mapper, axis=axis), or\n"
"\t.rename(index=index, columns=columns)")
if axis == 'index':
index = mapper
elif axis == 'columns':
columns = mapper
elif all(x is not None for x in (mapper, index, columns)):
raise TypeError("Cannot specify all of 'mapper', 'index', and "
"'columns'. Specify 'mapper' and 'axis', or "
"'index' and 'columns'.")
elif axis is None and (mapper is not None and index is not None):
# Determine if they meant df.rename(idx_map, col_map)
if callable(index) or is_dict_like(index):
warnings.warn("Interpreting call to '.rename(a, b)' as "
"'.rename(index=a, columns=b)'. "
"Use keyword arguments to remove ambiguity.",
UserWarning)
index, columns = mapper, index
elif index is None and columns is None:
index = mapper

index, columns = self._validate_axis_style_args(mapper, 'mapper',
index, columns,
axis, 'rename')
return super(DataFrame, self).rename(index=index, columns=columns,
**kwargs)

Expand Down

0 comments on commit 1e62a0e

Please sign in to comment.