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

MultiIndex names attribute broken when using set_value() to expand DataFrame() #3742

Closed
thriveth opened this Issue Jun 3, 2013 · 4 comments

Comments

Projects
None yet
4 participants
@thriveth

thriveth commented Jun 3, 2013

I have raised the issue in this question on Stack Overflow, but I'm not sure it ever made it to the Pandas issue tracker.

I have a MultiIndex'ed DataFrame which I want to expand by using set_value(), but doing this destroys the names attribute of the index. This does not happen when setting the value of an already existing entry in the DataFrame. An easily reproducible example is to create the dataframe by:

lev1 = ['hans', 'hans', 'hans', 'grethe', 'grethe', 'grethe']
lev2 = ['1', '2', '3'] * 2
idx = pd.MultiIndex.from_arrays(
    [lev1, lev2], 
    names=['Name', 'Number'])
df = pd.DataFrame(
    np.random.randn(6, 4),
    columns=['one', 'two', 'three', 'four'],
    index=idx)
df = df.sortlevel()
df 

This shows a neat and nice object, just as I expected, with proper naming of the index columns. If I now run:

df.set_value(('grethe', '3'), 'one', 99.34)

the result is also as expected. But if I run:

df.set_value(('grethe', '4'), 'one', 99.34)

The column names of the index are gone, and the names attribute has been set to [None, None].

@jreback

This comment has been minimized.

Show comment
Hide comment
@jreback

jreback Jun 3, 2013

Contributor

marked as bug for 0.12

Contributor

jreback commented Jun 3, 2013

marked as bug for 0.12

@cpcloud

This comment has been minimized.

Show comment
Hide comment
@cpcloud

cpcloud Jun 27, 2013

Member

closing in favor of #4039 . will move example from here to there

Member

cpcloud commented Jun 27, 2013

closing in favor of #4039 . will move example from here to there

@cpcloud cpcloud closed this Jun 27, 2013

@thriveth

This comment has been minimized.

Show comment
Hide comment
@thriveth

thriveth Jun 27, 2013

Thanks for fixing this, people. I love Pandas!

thriveth commented Jun 27, 2013

Thanks for fixing this, people. I love Pandas!

@jtratner

This comment has been minimized.

Show comment
Hide comment
@jtratner

jtratner Jul 2, 2013

Contributor

FYI: Issue was that _expand_axes used astype('O'), which caused the MultiIndex to convert into an Index and lost all the naming attributes (because MultiIndex didn't define its own index). PR in #4039 will fix this by overriding astype method in MultiIndex.

Contributor

jtratner commented Jul 2, 2013

FYI: Issue was that _expand_axes used astype('O'), which caused the MultiIndex to convert into an Index and lost all the naming attributes (because MultiIndex didn't define its own index). PR in #4039 will fix this by overriding astype method in MultiIndex.

jtratner added a commit to jtratner/pandas that referenced this issue Aug 11, 2013

BUG/ENH: Make names, levels and labels properties.
* `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 pandas-dev#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 pandas-dev#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.

jreback added a commit that referenced this issue Aug 12, 2013

BUG/ENH: Make names, levels and labels properties.
* `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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment