44
44
45
45
46
46
def _get_objs_combined_axis (objs , intersect = False , axis = 0 , sort = True ):
47
- # Extract combined index: return intersection or union (depending on the
48
- # value of "intersect") of indexes on given axis, or None if all objects
49
- # lack indexes (e.g. they are numpy arrays)
47
+ """
48
+ Extract combined index: return intersection or union (depending on the
49
+ value of "intersect") of indexes on given axis, or None if all objects
50
+ lack indexes (e.g. they are numpy arrays).
51
+
52
+ Parameters
53
+ ----------
54
+ objs : list of objects
55
+ Each object will only be considered if it has a _get_axis
56
+ attribute.
57
+ intersect : bool, default False
58
+ If True, calculate the intersection between indexes. Otherwise,
59
+ calculate the union.
60
+ axis : {0 or 'index', 1 or 'outer'}, default 0
61
+ The axis to extract indexes from.
62
+ sort : bool, default True
63
+ Whether the result index should come out sorted or not.
64
+
65
+ Returns
66
+ -------
67
+ Index
68
+ """
50
69
obs_idxes = [obj ._get_axis (axis ) for obj in objs
51
70
if hasattr (obj , '_get_axis' )]
52
71
if obs_idxes :
@@ -68,6 +87,24 @@ def _get_distinct_objs(objs):
68
87
69
88
70
89
def _get_combined_index (indexes , intersect = False , sort = False ):
90
+ """
91
+ Return the union or intersection of indexes.
92
+
93
+ Parameters
94
+ ----------
95
+ indexes : list of Index or list objects
96
+ When intersect=True, do not accept list of lists.
97
+ intersect : bool, default False
98
+ If True, calculate the intersection between indexes. Otherwise,
99
+ calculate the union.
100
+ sort : bool, default False
101
+ Whether the result index should come out sorted or not.
102
+
103
+ Returns
104
+ -------
105
+ Index
106
+ """
107
+
71
108
# TODO: handle index names!
72
109
indexes = _get_distinct_objs (indexes )
73
110
if len (indexes ) == 0 :
@@ -91,6 +128,21 @@ def _get_combined_index(indexes, intersect=False, sort=False):
91
128
92
129
93
130
def _union_indexes (indexes , sort = True ):
131
+ """
132
+ Return the union of indexes.
133
+
134
+ The behavior of sort and names is not consistent.
135
+
136
+ Parameters
137
+ ----------
138
+ indexes : list of Index or list objects
139
+ sort : bool, default True
140
+ Whether the result index should come out sorted or not.
141
+
142
+ Returns
143
+ -------
144
+ Index
145
+ """
94
146
if len (indexes ) == 0 :
95
147
raise AssertionError ('Must have at least 1 Index to union' )
96
148
if len (indexes ) == 1 :
@@ -102,6 +154,19 @@ def _union_indexes(indexes, sort=True):
102
154
indexes , kind = _sanitize_and_check (indexes )
103
155
104
156
def _unique_indices (inds ):
157
+ """
158
+ Convert indexes to lists and concatenate them, removing duplicates.
159
+
160
+ The final dtype is inferred.
161
+
162
+ Parameters
163
+ ----------
164
+ inds : list of Index or list objects
165
+
166
+ Returns
167
+ -------
168
+ Index
169
+ """
105
170
def conv (i ):
106
171
if isinstance (i , Index ):
107
172
i = i .tolist ()
@@ -140,6 +205,26 @@ def conv(i):
140
205
141
206
142
207
def _sanitize_and_check (indexes ):
208
+ """
209
+ Verify the type of indexes and convert lists to Index.
210
+
211
+ Cases:
212
+
213
+ - [list, list, ...]: Return ([list, list, ...], 'list')
214
+ - [list, Index, ...]: Return _sanitize_and_check([Index, Index, ...])
215
+ Lists are sorted and converted to Index.
216
+ - [Index, Index, ...]: Return ([Index, Index, ...], TYPE)
217
+ TYPE = 'special' if at least one special type, 'array' otherwise.
218
+
219
+ Parameters
220
+ ----------
221
+ indexes : list of Index or list objects
222
+
223
+ Returns
224
+ -------
225
+ sanitized_indexes : list of Index or list objects
226
+ type : {'list', 'array', 'special'}
227
+ """
143
228
kinds = list ({type (index ) for index in indexes })
144
229
145
230
if list in kinds :
@@ -158,6 +243,21 @@ def _sanitize_and_check(indexes):
158
243
159
244
160
245
def _get_consensus_names (indexes ):
246
+ """
247
+ Give a consensus 'names' to indexes.
248
+
249
+ If there's exactly one non-empty 'names', return this,
250
+ otherwise, return empty.
251
+
252
+ Parameters
253
+ ----------
254
+ indexes : list of Index objects
255
+
256
+ Returns
257
+ -------
258
+ list
259
+ A list representing the consensus 'names' found.
260
+ """
161
261
162
262
# find the non-none names, need to tupleify to make
163
263
# the set hashable, then reverse on return
@@ -169,6 +269,18 @@ def _get_consensus_names(indexes):
169
269
170
270
171
271
def _all_indexes_same (indexes ):
272
+ """
273
+ Determine if all indexes contain the same elements.
274
+
275
+ Parameters
276
+ ----------
277
+ indexes : list of Index objects
278
+
279
+ Returns
280
+ -------
281
+ bool
282
+ True if all indexes contain the same elements, False otherwise.
283
+ """
172
284
first = indexes [0 ]
173
285
for index in indexes [1 :]:
174
286
if not first .equals (index ):
0 commit comments