-
-
Notifications
You must be signed in to change notification settings - Fork 17.9k
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
Conversation
Added signature re-writing for python3 only. AFAICT, there's not a simple way to do that for both python 2 and 3. |
Codecov Report
@@ Coverage Diff @@
## master #17966 +/- ##
==========================================
- Coverage 91.26% 91.23% -0.04%
==========================================
Files 163 163
Lines 50117 50165 +48
==========================================
+ Hits 45739 45766 +27
- Misses 4378 4399 +21
Continue to review full report at Codecov.
|
In principle this is fine for me (anyhow, the solution to interpret it as positional index=, columns= but warning is good), but have the feeling it is more complex as needed? |
I’ll see if that works, it would be more maintainable. One drawback of that is having to treat a user explicitly passing labels=foo, index=bar the same as just passing two positional args. Currently we would raise on that, instead we would issue a warning.
…________________________________
From: Joris Van den Bossche <notifications@github.com>
Sent: Tuesday, October 24, 2017 5:23:14 PM
To: pandas-dev/pandas
Cc: Tom Augspurger; Author
Subject: Re: [pandas-dev/pandas] [WIP]Rename signature fix (#17966)
In principle this is fine for me (anyhow, the solution to interpret it as positional index=, columns= but warning is good), but have the feeling it is more complex as needed?
In the case the user passes positional arguments index, columns, this is captured as labels=index, index=columns, but specifying both labels as index is not valid? So normally we should raise for that case, but can't we catch this situation, reinterprete and do the warning as in this PR? Or am I missing something?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub<#17966 (comment)>, or mute the thread<https://github.com/notifications/unsubscribe-auth/ABQHIhWt100aiLR515jXWmupANBLJcERks5svmNSgaJpZM4QE7Qm>.
|
Ah, yes, that's a case I didn't think of. Personally, I would go for accepting that minor disadvantage to have the simpler code. The question then maybe is if we would make the warning stronger in mentioning that this will be changed in the future (actually deprecate instead of a UserWarning), so that in the future we can actually raise for the labels=foo, index=bar case |
|
||
sig = inspect.Signature(params) | ||
|
||
func.__signature__ = sig |
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.
cool that u can do this
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 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.
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.
I briefly tried that, but had to add several if obj.ndim >= 3
inside internals. Got a bit messy.
Hmm I'm having trouble making the simple way (no In [15]: df.rename(None, str.lower) # <----- This is the problem one
Out[15]:
A B
a NaN NaN
b NaN NaN
In [16]: df.rename(None, index=str.lower)
Out[16]:
A B
a NaN NaN
b NaN NaN
In [17]: df.rename(mapper=None, index=str.lower)
Out[17]:
A B
a NaN NaN
b NaN NaN We want to treat |
Once we're python3 only, we could using keyword-only arguments I think: def rename(mapper=None, *, index=None, columns=None, axis=None, ...): I should be able to emit a warning whenever |
I personally don't mind doing that (just all treating them as positional to deal with [15]). There will be a warning in any case, so people doing one of those three cases will typically change their code to not have the warning anymore. |
I'd be fine with raising on Let me try using different defaults other than None to see if that helps at all... |
@TomAugspurger if it is too complicated, I am also fine with this though! It's working, and we can later on remove the hacks again. |
FWIW, I find this version of |
To be clear, I mainly meant the signature hacking, I didn't look at the changes in |
Ah, yeah. FWIW, I'm 51/49 on whether overwriting the signature is a good thing or not. It's going to confuse static type checkers anyway. I'm OK with it for now since it'll be removed once we're Python3 only. |
OK, this should be all good. |
pandas/core/generic.py
Outdated
warnings.warn(msg.format(method_name=method_name,), FutureWarning, | ||
stacklevel=4) | ||
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 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).
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.
This is only used by reindex/rename, and now is sits next to those functions. IMO it is easier to keep it that way.
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.
Now also looked at the rewritten validate_axis_args method. Looks good!
pandas/core/generic.py
Outdated
elif len(args) == 2: | ||
if 'axis' in kwargs: | ||
# Unambiguously wrong | ||
msg = "Cannot specify both {} and any of 'index' or 'columns'" |
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 {})
pandas/core/generic.py
Outdated
warnings.warn(msg.format(method_name=method_name,), FutureWarning, | ||
stacklevel=4) | ||
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 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.
@jreback are you OK with it in generic? I use |
I don't really like it, because this is really validation code. If you could move it would be much better (and forr sure pass in |
Moved to |
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.
lgtm
* Signature rewriting
* Signature rewriting
git diff upstream/master -u -- "*.py" | flake8 --diff
Sample usage:
I'd like to simplify this further if possible, things go messy :/
I'd also like to fix the function signature, which is doable on python3 at least.