-
-
Notifications
You must be signed in to change notification settings - Fork 18.5k
CLN: reorganize index.py, test_index.py #12124
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
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9cca3c5
Split pandas.core.index into a pandas/indexes subpackage
wesm f73c17b
Remove float.py
wesm f464384
Reorganize pandas/tests/test_index.py
wesm 49da0e4
Fix pickling
wesm a1d4d07
Fix Python 2 issues
wesm 9b10283
Address comments
wesm d87eb63
Add TODO about private names
wesm a2e6ec6
Fix Python 2 unicode issue
wesm 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
Large diffs are not rendered by default.
Oops, something went wrong.
Empty file.
This file contains hidden or 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,118 @@ | ||
from pandas.indexes.base import (Index, _new_Index, # noqa | ||
_ensure_index, _get_na_value, | ||
InvalidIndexError) | ||
from pandas.indexes.category import CategoricalIndex # noqa | ||
from pandas.indexes.multi import MultiIndex # noqa | ||
from pandas.indexes.numeric import (NumericIndex, Float64Index, # noqa | ||
Int64Index) | ||
from pandas.indexes.range import RangeIndex # noqa | ||
|
||
import pandas.core.common as com | ||
import pandas.lib as lib | ||
|
||
# TODO: there are many places that rely on these private methods existing in | ||
# pandas.core.index | ||
__all__ = ['Index', 'MultiIndex', 'NumericIndex', 'Float64Index', 'Int64Index', | ||
'CategoricalIndex', 'RangeIndex', | ||
'InvalidIndexError', | ||
'_new_Index', | ||
'_ensure_index', '_get_na_value', '_get_combined_index', | ||
'_get_distinct_indexes', '_union_indexes', | ||
'_get_consensus_names', | ||
'_all_indexes_same'] | ||
|
||
|
||
def _get_combined_index(indexes, intersect=False): | ||
# TODO: handle index names! | ||
indexes = _get_distinct_indexes(indexes) | ||
if len(indexes) == 0: | ||
return Index([]) | ||
if len(indexes) == 1: | ||
return indexes[0] | ||
if intersect: | ||
index = indexes[0] | ||
for other in indexes[1:]: | ||
index = index.intersection(other) | ||
return index | ||
union = _union_indexes(indexes) | ||
return _ensure_index(union) | ||
|
||
|
||
def _get_distinct_indexes(indexes): | ||
return list(dict((id(x), x) for x in indexes).values()) | ||
|
||
|
||
def _union_indexes(indexes): | ||
if len(indexes) == 0: | ||
raise AssertionError('Must have at least 1 Index to union') | ||
if len(indexes) == 1: | ||
result = indexes[0] | ||
if isinstance(result, list): | ||
result = Index(sorted(result)) | ||
return result | ||
|
||
indexes, kind = _sanitize_and_check(indexes) | ||
|
||
def _unique_indices(inds): | ||
def conv(i): | ||
if isinstance(i, Index): | ||
i = i.tolist() | ||
return i | ||
|
||
return Index(lib.fast_unique_multiple_list([conv(i) for i in inds])) | ||
|
||
if kind == 'special': | ||
result = indexes[0] | ||
|
||
if hasattr(result, 'union_many'): | ||
return result.union_many(indexes[1:]) | ||
else: | ||
for other in indexes[1:]: | ||
result = result.union(other) | ||
return result | ||
elif kind == 'array': | ||
index = indexes[0] | ||
for other in indexes[1:]: | ||
if not index.equals(other): | ||
return _unique_indices(indexes) | ||
|
||
return index | ||
else: | ||
return _unique_indices(indexes) | ||
|
||
|
||
def _sanitize_and_check(indexes): | ||
kinds = list(set([type(index) for index in indexes])) | ||
|
||
if list in kinds: | ||
if len(kinds) > 1: | ||
indexes = [Index(com._try_sort(x)) | ||
if not isinstance(x, Index) else | ||
x for x in indexes] | ||
kinds.remove(list) | ||
else: | ||
return indexes, 'list' | ||
|
||
if len(kinds) > 1 or Index not in kinds: | ||
return indexes, 'special' | ||
else: | ||
return indexes, 'array' | ||
|
||
|
||
def _get_consensus_names(indexes): | ||
|
||
# find the non-none names, need to tupleify to make | ||
# the set hashable, then reverse on return | ||
consensus_names = set([tuple(i.names) for i in indexes | ||
if all(n is not None for n in i.names)]) | ||
if len(consensus_names) == 1: | ||
return list(list(consensus_names)[0]) | ||
return [None] * indexes[0].nlevels | ||
|
||
|
||
def _all_indexes_same(indexes): | ||
first = indexes[0] | ||
for index in indexes[1:]: | ||
if not first.equals(index): | ||
return False | ||
return True |
Oops, something went wrong.
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.
any reason you are expliciting adding the 'private' index methods to the import space?
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.
Other modules rely on these names existing in
pandas.core.index
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.
hmm, then they really should be importing directly (if you can gr8, otherwise pls add a TODO)
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.
done