Skip to content

Commit 49c0023

Browse files
wesmjreback
authored andcommitted
CLN: reorganize index.py, test_index.py
Split apart these two very large modules and created a new `pandas.indexes` subpackage. It would be nice to move all the index class code from `pandas.tseries` there in a followup patch. Author: Wes McKinney <[email protected]> Closes #12124 from wesm/reorg/indexes and squashes the following commits: a2e6ec6 [Wes McKinney] Fix Python 2 unicode issue d87eb63 [Wes McKinney] Add TODO about private names 9b10283 [Wes McKinney] Address comments a1d4d07 [Wes McKinney] Fix Python 2 issues 49da0e4 [Wes McKinney] Fix pickling f464384 [Wes McKinney] Reorganize pandas/tests/test_index.py f73c17b [Wes McKinney] Remove float.py 9cca3c5 [Wes McKinney] Split pandas.core.index into a pandas/indexes subpackage
1 parent ab3291d commit 49c0023

22 files changed

+14438
-14227
lines changed

pandas/core/index.py

+3-7,081
Large diffs are not rendered by default.

pandas/indexes/__init__.py

Whitespace-only changes.

pandas/indexes/api.py

+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
from pandas.indexes.base import (Index, _new_Index, # noqa
2+
_ensure_index, _get_na_value,
3+
InvalidIndexError)
4+
from pandas.indexes.category import CategoricalIndex # noqa
5+
from pandas.indexes.multi import MultiIndex # noqa
6+
from pandas.indexes.numeric import (NumericIndex, Float64Index, # noqa
7+
Int64Index)
8+
from pandas.indexes.range import RangeIndex # noqa
9+
10+
import pandas.core.common as com
11+
import pandas.lib as lib
12+
13+
# TODO: there are many places that rely on these private methods existing in
14+
# pandas.core.index
15+
__all__ = ['Index', 'MultiIndex', 'NumericIndex', 'Float64Index', 'Int64Index',
16+
'CategoricalIndex', 'RangeIndex',
17+
'InvalidIndexError',
18+
'_new_Index',
19+
'_ensure_index', '_get_na_value', '_get_combined_index',
20+
'_get_distinct_indexes', '_union_indexes',
21+
'_get_consensus_names',
22+
'_all_indexes_same']
23+
24+
25+
def _get_combined_index(indexes, intersect=False):
26+
# TODO: handle index names!
27+
indexes = _get_distinct_indexes(indexes)
28+
if len(indexes) == 0:
29+
return Index([])
30+
if len(indexes) == 1:
31+
return indexes[0]
32+
if intersect:
33+
index = indexes[0]
34+
for other in indexes[1:]:
35+
index = index.intersection(other)
36+
return index
37+
union = _union_indexes(indexes)
38+
return _ensure_index(union)
39+
40+
41+
def _get_distinct_indexes(indexes):
42+
return list(dict((id(x), x) for x in indexes).values())
43+
44+
45+
def _union_indexes(indexes):
46+
if len(indexes) == 0:
47+
raise AssertionError('Must have at least 1 Index to union')
48+
if len(indexes) == 1:
49+
result = indexes[0]
50+
if isinstance(result, list):
51+
result = Index(sorted(result))
52+
return result
53+
54+
indexes, kind = _sanitize_and_check(indexes)
55+
56+
def _unique_indices(inds):
57+
def conv(i):
58+
if isinstance(i, Index):
59+
i = i.tolist()
60+
return i
61+
62+
return Index(lib.fast_unique_multiple_list([conv(i) for i in inds]))
63+
64+
if kind == 'special':
65+
result = indexes[0]
66+
67+
if hasattr(result, 'union_many'):
68+
return result.union_many(indexes[1:])
69+
else:
70+
for other in indexes[1:]:
71+
result = result.union(other)
72+
return result
73+
elif kind == 'array':
74+
index = indexes[0]
75+
for other in indexes[1:]:
76+
if not index.equals(other):
77+
return _unique_indices(indexes)
78+
79+
return index
80+
else:
81+
return _unique_indices(indexes)
82+
83+
84+
def _sanitize_and_check(indexes):
85+
kinds = list(set([type(index) for index in indexes]))
86+
87+
if list in kinds:
88+
if len(kinds) > 1:
89+
indexes = [Index(com._try_sort(x))
90+
if not isinstance(x, Index) else
91+
x for x in indexes]
92+
kinds.remove(list)
93+
else:
94+
return indexes, 'list'
95+
96+
if len(kinds) > 1 or Index not in kinds:
97+
return indexes, 'special'
98+
else:
99+
return indexes, 'array'
100+
101+
102+
def _get_consensus_names(indexes):
103+
104+
# find the non-none names, need to tupleify to make
105+
# the set hashable, then reverse on return
106+
consensus_names = set([tuple(i.names) for i in indexes
107+
if all(n is not None for n in i.names)])
108+
if len(consensus_names) == 1:
109+
return list(list(consensus_names)[0])
110+
return [None] * indexes[0].nlevels
111+
112+
113+
def _all_indexes_same(indexes):
114+
first = indexes[0]
115+
for index in indexes[1:]:
116+
if not first.equals(index):
117+
return False
118+
return True

0 commit comments

Comments
 (0)