Skip to content

Commit 9cca3c5

Browse files
committed
Split pandas.core.index into a pandas/indexes subpackage
1 parent 0b8fa9d commit 9cca3c5

File tree

9 files changed

+7183
-7081
lines changed

9 files changed

+7183
-7081
lines changed

pandas/core/index.py

Lines changed: 3 additions & 7081 deletions
Large diffs are not rendered by default.

pandas/indexes/__init__.py

Whitespace-only changes.

pandas/indexes/api.py

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

0 commit comments

Comments
 (0)