-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
Weird MultiIndex bug #3714
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
Comments
When you copy the data, you get the same identical index, do
the problem is that you are changing the levels on a MultiIndex, which is an immutable object This is a bug in that assigning to levels should raise |
@wesm we do allow assigning to the |
it definitely is confusing the way pandas behaves right now. so I would have to do something like
or is there a more convenient way to do this? |
use set/reset index
|
I don't know the internals but this sounds pretty expensive given that I Doesn't this first construct new columns, then I operate on a whole column On Mon, Jun 3, 2013 at 4:35 PM, jreback notifications@github.com wrote:
|
this is pretty cheap to do; see if this actually a bottleneck for you what is your end goal? |
these ops can also be done inplace, fyi (pass |
|
I have some situations where I want to change some values in the levels and in the labels. |
you can try to copy the index, then assign to the levels, then set the index e.g.
to avoid the aliasing issue (this way you make sure that the index you have is ONLY attached to where you want it |
much better, thanks! |
great! this is still a 'bug' in any event, so will keep this issue open |
ok. just for me to understand:
supposed to fail in the future since we still access the index.levels ? in this case my previously suggested (clumsy) method would be safer to use On Mon, Jun 3, 2013 at 5:47 PM, jreback notifications@github.com wrote:
|
At some point I think we might raise if you try to set level directly, so your solution bypasses that, but in the meantime go ahead and use it |
* `names` is now a property *and* is set up as an immutable tuple. * `levels` are always (shallow) copied now and it is deprecated to set directly * `labels` are set up as a property now, moving all the processing of labels out of `__new__` + shallow-copied. * `levels` and `labels` are immutable. * Add names tests, motivating example from #3742, reflect tuple-ish output from names, and level names check to reindex test. * Add set_levels, set_labels, set_names and rename to index * Deprecate setting labels and levels directly Similar to other set_* methods...allows mutation if necessary but otherwise returns same object. Labels are now converted to `FrozenNDArray` and wrapped in a `FrozenList`. Should mostly resolve #3714 because you have to work to actually make assignments to an `Index`. BUG: Give MultiIndex its own astype method Fixes issue with set_value forgetting names.
Bug: Assigning levels/labels to a multiindex (or really any fields in Index) should raise (if done externally)
This is (for me) one of the weirdest things I have found so far (0.10.1 still)
tl;dr
it seems like df.copy() creates a shallow copy of the MultiIndex w.r.t levels
Also, the setting of the index.levels does not seem to have effect on the index itself
Running the same code twice results in different results during the second time
although I do not alter the original object in any place.
also note the line "print data.index.levels[1]" which changes after changing data2
my original goal (concatenating) always contains the original date in all rows
Code
Output:
The text was updated successfully, but these errors were encountered: