-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Fix multiindex selection #2621
Merged
Merged
Fix multiindex selection #2621
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
50eab43
Fix multiindex selection
fujiisoup 762f496
Support pandas0.19
fujiisoup 6bb8166
a bugfix
fujiisoup a806c64
Do remove_unused_levels only once in unstack.
fujiisoup 205f948
import algos
fujiisoup b15cab3
Remove unused import
fujiisoup edb4a24
Adopt local import
fujiisoup 61d1d49
Merge branch 'master' into multiindex_remove_unused
fujiisoup File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
import numpy as np | ||
import pandas as pd | ||
import pandas.core.algorithms as algos | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you copy the pandas license directly into this file, too? See There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
# for pandas 0.19 | ||
def remove_unused_levels(self): | ||
""" | ||
create a new MultiIndex from the current that removing | ||
unused levels, meaning that they are not expressed in the labels | ||
The resulting MultiIndex will have the same outward | ||
appearance, meaning the same .values and ordering. It will also | ||
be .equals() to the original. | ||
.. versionadded:: 0.20.0 | ||
Returns | ||
------- | ||
MultiIndex | ||
Examples | ||
-------- | ||
>>> i = pd.MultiIndex.from_product([range(2), list('ab')]) | ||
MultiIndex(levels=[[0, 1], ['a', 'b']], | ||
labels=[[0, 0, 1, 1], [0, 1, 0, 1]]) | ||
>>> i[2:] | ||
MultiIndex(levels=[[0, 1], ['a', 'b']], | ||
labels=[[1, 1], [0, 1]]) | ||
The 0 from the first level is not represented | ||
and can be removed | ||
>>> i[2:].remove_unused_levels() | ||
MultiIndex(levels=[[1], ['a', 'b']], | ||
labels=[[0, 0], [0, 1]]) | ||
""" | ||
|
||
new_levels = [] | ||
new_labels = [] | ||
|
||
changed = False | ||
for lev, lab in zip(self.levels, self.labels): | ||
|
||
# Since few levels are typically unused, bincount() is more | ||
# efficient than unique() - however it only accepts positive values | ||
# (and drops order): | ||
uniques = np.where(np.bincount(lab + 1) > 0)[0] - 1 | ||
has_na = int(len(uniques) and (uniques[0] == -1)) | ||
|
||
if len(uniques) != len(lev) + has_na: | ||
# We have unused levels | ||
changed = True | ||
|
||
# Recalculate uniques, now preserving order. | ||
# Can easily be cythonized by exploiting the already existing | ||
# "uniques" and stop parsing "lab" when all items are found: | ||
uniques = algos.unique(lab) | ||
if has_na: | ||
na_idx = np.where(uniques == -1)[0] | ||
# Just ensure that -1 is in first position: | ||
uniques[[0, na_idx[0]]] = uniques[[na_idx[0], 0]] | ||
|
||
# labels get mapped from uniques to 0:len(uniques) | ||
# -1 (if present) is mapped to last position | ||
label_mapping = np.zeros(len(lev) + has_na) | ||
# ... and reassigned value -1: | ||
label_mapping[uniques] = np.arange(len(uniques)) - has_na | ||
|
||
lab = label_mapping[lab] | ||
|
||
# new levels are simple | ||
lev = lev.take(uniques[has_na:]) | ||
|
||
new_levels.append(lev) | ||
new_labels.append(lab) | ||
|
||
result = self._shallow_copy() | ||
|
||
if changed: | ||
result._reset_identity() | ||
result._set_levels(new_levels, validate=False) | ||
result._set_labels(new_labels, validate=False) | ||
|
||
return result |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can we do a local import here instead the function?
I'm a little nervous that some future version of pandas may drop this (private) module, which would then break imports even though this isn't actually used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch! Fixed.