-
-
Notifications
You must be signed in to change notification settings - Fork 18.3k
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
Arithmetic by DataFrame index #7439
Comments
why are you using a Series in your example and not a DataFrame? have you tried reset_index()? this is exactly tht purpose |
I did not mean to only subtract the first value, i used it only as example. As for reason why i'm using Series; it's the same problem for dataframes, i just needed simple example. |
What are you actually trying to do? You simply need to use |
I want to use index to make some calculations on it, without changing dataframe or adding another column to it. |
I get that, pls show an example of what you want to do. The above does not show what you are saying. |
|
simply convert to series and it will work
|
@jreback pandas is not entirely set-like with index math. For example, subtracting a DateOffset objects does work:
The same is true for numeric indices. Compare:
In my opinion, since Index is ndarray like, it would be less surprising if Index only supported math operations like an ndarray rather than like a set. Does anyone really use the overloaded operators for set operations? At the very least, pandas should pick one. Supporting both results in some very ambiguous cases (like my 2nd and 3rd examples). |
@shoyer this has long been an issue. The problem is union further since Index are very much like Series you expect addition type ops to a I don't see a problem with adding/subtracting a so it IS type sensitive. Not sure what the answer is here. I think changing this could be a problem, its pretty ingrained. That said if someone wants to come up with a (better!) rational scheme, could take a look. |
My solution would be to only have named methods for set operations ( This seems much more Pythonic to me: "In the face of ambiguity, refuse the temptation to guess." It would require a long deprecation cycle, but eventually we could fix cases like this issue. The current dual purpose operator overloading is clearly confusing to new users -- and I expect even for expert users in some cases. For what it's worth, personally I do more index math than set operations. But ultimately which operation to use for infix operators comes down to whether Index is more set-like or ndarray-like, and for which functionality there are obvious alternatives. I would argue that Index is a bit more ndarray-like and infix notation is also much more obvious/standard for arrays than sets. |
maybe but then you lose this, which IMHO is prob the most used
|
There is a small section on this in the docs: http://pandas.pydata.org/pandas-docs/stable/indexing.html#set-operations-on-index-objects, but making a DOC issue of this for now? Eg adding a section in the 'gotchas' about this? |
yeh...let's make a doc issue for now / and/or think about this for 0.15 |
@jreback I think this can be closed now? The issue with set operations on index should be handled in the meantime? (they are deprecated for adding two indexes, and eg |
yep, no need for addtl validation tests as this is already tested pretty |
I encountered a problem with doing any arythmetic from index, in other words, when a index is time (datetime64) and i would like to count something by it, i have no other option than to assign it to some column in dataframe object.
import pandas as pd
result:
Maybe it's just conceptional problem, but if i want to make something with date index i have to keep additional column (with the same values as index!).
When it comes to huge datasets this can be a problem, because i have to store the same thing twice, or make additional column for calculations, which is not better.
P.S. pd.show_versions():
The text was updated successfully, but these errors were encountered: