-
-
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
API: make '+' and '-' for Index either do numeric operations of raise TypeError (instead of setops) #8226
Comments
can easily just deprecate this in 0.15.0 (and then start raising in say 0.16.0), or could raise in 0.15.0. |
+1 from me (obviously, this was sort of my idea). There are a few other issues where this has come up (including a bug report/complaint) but I can't find them now. |
I have a PR for this, its really a tiny change (internally was only used in 2 routines). I honestly don't think its a bit deal. Still open to simply warn (rather than raise) though (or can just break it). |
It makes sense to make set operations secondary to arithmetics, so +1 from me as well. As for warn/raise, I'd say a graceful deprecation (a pre-change warning for one release |
I think this should be deprecated first (warning), as this really breaks your code and is used (I used it myself also), how strange it could be at first sight. But I see a point in making this less confusing. Some questions:
|
Agree with the spirit of the PR and that we should warn for a release or two. @jorisvandenbossche I think + and - will always do arithmetic ops. Then idx.union and idx.diff are for setops. What does |
|
AFAIK python sets don't support |
anyone negative on |
It seems a clear rule that numerical operations on Index do arithmetic, and bitwise operations do set-like things (and comparison operations do elementwise comparison). But in numpy, bitwise operations also have gotten another meaning than in standard python (and do elementwise boolean operations), so in that regard you can still see it as a bit surprising. |
I think least surprise principle is in effect. Some operations are SO compelling on indexes that they HAVE to be numeric (dti + tdi, dti - dti, int64index + int64index). and union/difference are just not in the thought train. |
@jorisvandenbossche are we using |
|
re: diff -> difference re: bitwise ops I wonder if anyone considered another option to solve this inconsistency in exactly the opposite way, given that Index is already separated from ndarray, so that operations on Index always have set semantics and if one is going for element-wise arithmetics they should use |
@immerrr In theory having set semantics is nice for
thus the impetus for the change. And to avoid ambiguity on index types (e.g. can't use To be honest, dropping BTW in set ops (python), is |
Ok, got that, indeed
Yup, a proper numpy dtype (or ndarray subclass) to do exactly that is still on my "someday" todo list :) |
@jreback like @jorisvandenbossche said, python set ops don't even implement I was thinking about the binary operations issue. I agree that from a consistency perspective, |
Here's a related API consistency issue -- what should the type of the object returned from comparisons and arithmetic operations with an index be?
I doubt the index functionality is actually desired or necessary most of the time, and if it is necessary, a ndarray could be coerced to an index anyways. What about making On the other hand, there would indeed be some small type detection penalty when casting to an index (as @immerrr points out), and from a practical perspective, TimedeltaIndex and DatetimeIndex are much more functional that Thoughts? |
Personally I am -1 on returning an array from arithmetic operations on the Index, then you can better use |
currently Index comparison ops return a boolean array. We could have a ok, going to merge the PR unless objections? (I need to this to go before the tdi changes). |
To summarise what you did in the PR:
That's right? And what in the future? Error on |
I would eventually raise on NotIMplemented right now is |
OK, I thought I would just raise the possibility of Thanks @jreback for resolving this! |
The Index set operations + and - were deprecated in order to provide these for numeric type operations on certain index types. + can be replaced by .union() or |, and - by .difference(). Further the method name Index.diff() is deprecated and can be replaced by Index.difference() (GH8226) pandas-dev/pandas#8226
see comments at the end of #8184
so this type of syntax
index1 + index2
would be replaced by
index1.union(index2)
and
index1 - index2
by
index1.diff(index2)
Their is some internal code to fix but not a lot.
This would go a long ways toward simplifying the user API for index ops. I think.
The text was updated successfully, but these errors were encountered: