Skip to content
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

'Mixed depth' hierarchical column labels #768

Closed
wants to merge 3 commits into from

Conversation

@chrisjbillington
Copy link
Contributor

commented Feb 9, 2012

Modifications to column-hierarchical behaviour as per "'Mixed depth' hierarchical column labels" discussion on pystatsmodels.

If column label tuples contain only empty strings at the lower levels, allows the DataFrame to be indexed with only the higher levels. So for example if a column has the hierarchical label ('a','',''), and there are no other columns with 'a' at their top level, then this change allows the column to be pulled out with df['a'], rather than df['a','','']. It also names the resulting series 'a'.
The use case for this is when some columns have a deeper hierarchy than others, and you want to treat the shallower columns as if the lower levels of the hierarchy weren't there.

If column label tuples contain only empty strings at the lower levels, allows the DataFrame to be indexed with only the higher levels. So for example if a column has the hierarchical label ('a','',''), and there are no other columns with 'a' at their top level, then this change allows the column to be pulled out with df['a'], rather than df['a','','']. It also names the resulting series 'a'.
The use case for this is when some columns have a deeper hierarchy than others, and you want to treat the shallower columns as if the lower levels of the hierarchy weren't there.

Modifications to column-hierarchical behaviour as per "'Mixed depth' …
…hierarchical column labels'" discussion on pystatsmodels.

If column label tuples contain only empty strings at the lower levels, allows the DataFrame to be indexed with only the higher levels. So for example if a column has the hierarchical label ('a','',''), and there are no other columns with 'a' at their top level, then this change allows the column to be pulled out with df['a'], rather than df['a','','']. It also names the resulting series 'a'. 
The use case for this is when some columns have a deeper hierarchy than others, and you want to treat the shallower columns as if the lower levels of the hierarchy weren't there.
@wesm

This comment has been minimized.

Copy link
Member

commented Feb 9, 2012

Could I ask you to add a couple of tests? pandas/tests/test_multilevel.py would probably be the right place

@chrisjbillington

This comment has been minimized.

Copy link
Contributor Author

commented Feb 9, 2012

Sure, it will take me a short while to figure out the test framework, but as I'm sure you'll attest it's something I should be doing anyway!

@wesm

This comment has been minimized.

Copy link
Member

commented Feb 9, 2012

Well, there's not much to it. You already did the hard part which was modifying the code so that your use case works-- so take your test case, put that in a test function, and throw in some asserts that the results are what you expect

Tests for 'mixed depth' hierarchical columns.
Tests for getting, setting, dropping and popping columns from dataframes which have MultiIndexed columns containing some empty strings at the lower levels, by indexing with only the upper levels.
Also modified NDFrame._set_item to use the same shorthand.
Fixed regression with Panels caused by NDFrame not checking if it was…
… DataFrame before assuming it had a 'columns' attribute.
@chrisjbillington

This comment has been minimized.

Copy link
Contributor Author

commented Feb 9, 2012

Ah, sorry, earlier commits broke other tests. All tests now pass...except for test_daterange.TestDateRange.test_with_tzinfo_ambiguous_times, but that fails for me without my changes.

@wesm

This comment has been minimized.

Copy link
Member

commented Feb 9, 2012

thanks Chris, this looks great. I imagine that test is failing because of missing or outdated pytz

@wesm wesm closed this Feb 9, 2012

@chrisjbillington

This comment has been minimized.

Copy link
Contributor Author

commented Feb 10, 2012

Cheers! Thanks for including it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.