-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
Rename signature fix #17966
Rename signature fix #17966
Changes from 5 commits
05efc51
7c419e0
fbf246b
ef77ca1
44e01da
396a454
3cb3fac
4766c24
ccbd75e
d01aa20
1c1517a
06144af
edcbb9a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,7 +28,7 @@ | |
from pandas.core.dtypes.cast import maybe_promote, maybe_upcast_putmask | ||
from pandas.core.dtypes.missing import isna, notna | ||
from pandas.core.dtypes.generic import ABCSeries, ABCPanel, ABCDataFrame | ||
from pandas.core.common import (_all_not_none, _count_not_none, | ||
from pandas.core.common import (_count_not_none, | ||
_maybe_box_datetimelike, _values_from_object, | ||
AbstractMethodError, SettingWithCopyError, | ||
SettingWithCopyWarning) | ||
|
@@ -728,51 +728,6 @@ def swaplevel(self, i=-2, j=-1, axis=0): | |
result._data.set_axis(axis, labels.swaplevel(i, j)) | ||
return result | ||
|
||
def _validate_axis_style_args(self, arg, arg_name, axes, | ||
axis, method_name): | ||
out = {} | ||
for i, value in enumerate(axes): | ||
if value is not None: | ||
out[self._AXIS_NAMES[i]] = value | ||
|
||
aliases = ', '.join(self._AXIS_NAMES.values()) | ||
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 any(x is not None for x in axes): | ||
msg = ( | ||
"Can't specify both 'axis' and {aliases}. " | ||
"Specify either\n" | ||
"\t.{method_name}({arg_name}, axis=axis), or\n" | ||
"\t.{method_name}(index=index, columns=columns)" | ||
).format(arg_name=arg_name, method_name=method_name, | ||
aliases=aliases) | ||
raise TypeError(msg) | ||
out[axis] = arg | ||
|
||
elif _all_not_none(arg, *axes): | ||
msg = ( | ||
"Cannot specify all of '{arg_name}', {aliases}. " | ||
"Specify either {arg_name} and 'axis', or {aliases}." | ||
).format(arg_name=arg_name, aliases=aliases) | ||
raise TypeError(msg) | ||
|
||
elif _all_not_none(arg, axes[0]): | ||
# This is the "ambiguous" case, so emit a warning | ||
msg = ( | ||
"Interpreting call to '.{method_name}(a, b)' as " | ||
"'.{method_name}(index=a, columns=b)'. " # TODO | ||
"Use keyword arguments to remove any ambiguity." | ||
).format(method_name=method_name) | ||
warnings.warn(msg, stacklevel=3) | ||
out[self._AXIS_ORDERS[0]] = arg | ||
out[self._AXIS_ORDERS[1]] = axes[0] | ||
elif axes[0] is None: | ||
# This is for the default axis, like reindex([0, 1]) | ||
out[self._AXIS_ORDERS[0]] = arg | ||
return out | ||
|
||
# ---------------------------------------------------------------------- | ||
# Rename | ||
|
||
|
@@ -2822,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'" | ||
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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would really love to move this out of generic (and just pass There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I briefly tried that, but had to add several |
||
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) | ||
|
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(and maybe ' ' around {})