8000 Rename signature fix by TomAugspurger · Pull Request #17966 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

Rename signature fix #17966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Oct 27, 2017
Prev Previous commit
Next Next commit
Do for panel too
  • Loading branch information
TomAugspurger committed Oct 24, 2017
commit 7c419e0fa6a9f74dd25697e3413067a0310798a3
51 changes: 0 additions & 51 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -3025,57 +3025,6 @@ def shift(self, periods=1, freq=None, axis=0):
return super(DataFrame, self).shift(periods=periods, freq=freq,
axis=axis)

def _validate_axis_style_args(self, args, kwargs, arg_name, method_name):
out = {}
# Goal: fill out with index/columns-style arguments
# like out = {'index': foo, 'columns': bar}
# Start by validaing for consistency
if 'axis' in kwargs and any(x in kwargs for x in self._AXIS_NUMBERS):
msg = "Cannot specify both 'axis' and any of 'columns' or 'index'."
raise TypeError(msg)

# Start with explicit values provided by the user...
if arg_name in kwargs:
if args:
msg = "{} got multiple values for argument '{}'".format(
method_name, arg_name)
raise TypeError(msg)
axis = self._get_axis_name(kwargs.get('axis', 0))
out[axis] = kwargs[arg_name]

# fill in axes
for k, v in kwargs.items():
try:
ax = self._get_axis_name(k)
except ValueError:
pass
else:
out[ax] = v

# All user-provided kwargs have been handled now.
# Now we supplement with positional arguments, raising when there's
# ambiguity

if len(args) == 0:
pass # validate later
elif len(args) == 1:
axis = self._get_axis_name(kwargs.get('axis', 0))
out[axis] = args[0]
elif len(args) == 2:
if 'axis' in kwargs:
msg = "Cannot specify both {} and any of 'index' or 'columns'"
raise TypeError(msg.format(arg_name))
msg = ("Intepreting call\n\t'.{method_name}(a, b)' as "
"\n\t'.{method_name}(index=a, columns=b)'.\nUse named "
"arguments to remove any ambiguity.")
warnings.warn(msg.format(method_name=method_name,), stacklevel=3)
out[self._AXIS_NAMES[0]] = args[0]
out[self._AXIS_NAMES[1]] = args[1]
else:
msg = "Cannot specify all of '{}', 'index', 'columns'."
raise TypeError(msg.format(arg_name))
return out

def set_index(self, keys, drop=True, append=False, inplace=False,
verify_integrity=False):
"""
Expand Down
51 changes: 51 additions & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2777,6 +2777,57 @@ def sort_index(self, axis=0, level=None, ascending=True, inplace=False,
new_axis = labels.take(sort_index)
return self.reindex(**{axis_name: new_axis})

def _validate_axis_style_args(self, args, kwargs, arg_name, method_name):
out = {}
# Goal: fill out with index/columns-style arguments
# like out = {'index': foo, 'columns': bar}
# Start by validaing for consistency
if 'axis' in kwargs and any(x in kwargs for x in self._AXIS_NUMBERS):
msg = "Cannot specify both 'axis' and any of 'columns' or 'index'."
raise TypeError(msg)

# Start with explicit values provided by the user...
if arg_name in kwargs:
if args:
msg = "{} got multiple values for argument '{}'".format(
method_name, arg_name)
raise TypeError(msg)
axis = self._get_axis_name(kwargs.get('axis', 0))
out[axis] = kwargs[arg_name]

# fill in axes
for k, v in kwargs.items():
try:
ax = self._get_axis_name(k)
except ValueError:
pass
else:
out[ax] = v

# All user-provided kwargs have been handled now.
# Now we supplement with positional arguments, raising when there's
# ambiguity

if len(args) == 0:
pass # validate later
elif len(args) == 1:
axis = self._get_axis_name(kwargs.get('axis', 0))
out[axis] = args[0]
elif len(args) == 2:
if 'axis' in kwargs:
msg = "Cannot specify both {} and any of 'index' or 'columns'"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this be "Cannot specify both axis and index / columns" ?(as you check that axis is specified)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(and maybe ' ' around {})

raise TypeError(msg.format(arg_name))
msg = ("Intepreting call\n\t'.{method_name}(a, b)' as "
"\n\t'.{method_name}(index=a, columns=b)'.\nUse named "
"arguments to remove any ambiguity.")
warnings.warn(msg.format(method_name=method_name,), stacklevel=3)
out[self._AXIS_NAMES[0]] = args[0]
out[self._AXIS_NAMES[1]] = args[1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would really love to move this out of generic (and just pass self into a static function, maybe into pandas.compat.validations or something).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is only used by reindex/rename, and now is sits next to those functions. IMO it is easier to keep it that way.

else:
msg = "Cannot specify all of '{}', 'index', 'columns'."
raise TypeError(msg.format(arg_name))
return out

_shared_docs['reindex'] = """
Conform %(klass)s to new index with optional filling logic, placing
NA/NaN in locations having no value in the previous index. A new object
Expand Down
31 changes: 21 additions & 10 deletions pandas/core/panel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1199,20 +1199,31 @@ def _wrap_result(self, result, axis):
return self._construct_return_type(result, axes)

@Appender(_shared_docs['reindex'] % _shared_doc_kwargs)
def reindex(self, labels=None,
items=None, major_axis=None, minor_axis=None,
axis=None, **kwargs):
major_axis = (major_axis if major_axis is not None else
kwargs.pop('major', None))
minor_axis = (minor_axis if minor_axis is not None else
kwargs.pop('minor', None))
axes = self._validate_axis_style_args(
labels, 'labels', axes=[items, major_axis, minor_axis],
axis=axis, method_name='reindex')
def reindex(self, *args, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it might actually make this more complicated, but would be ok with leaving panel/panel4d as is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I briefly tried that, but had to add several if obj.ndim >= 3 inside internals. Got a bit messy.

major = kwargs.pop("major", None)
minor = kwargs.pop('minor', None)

if major is not None:
if kwargs.get("major_axis"):
raise TypeError("Cannot specify both 'major' and 'major_axis'")
kwargs['major_axis'] = major
if minor is not None:
if kwargs.get("minor_axis"):
raise TypeError("Cannot specify both 'minor' and 'minor_axis'")

kwargs['minor_axis'] = minor
# major_axis = (major_axis if major_axis is not None else
# kwargs.pop('major', None))
# minor_axis = (minor_axis if minor_axis is not None else
# kwargs.pop('minor', None))
axes = self._validate_axis_style_args(args, kwargs, 'labels',
'reindex')
if self.ndim >= 4:
# Hack for PanelND
axes = {}
kwargs.update(axes)
kwargs.pop('axis', None)
kwargs.pop('labels', None)
return super(Panel, self).reindex(**kwargs)

@Appender(_shared_docs['rename'] % _shared_doc_kwargs)
Expand Down
0