Skip to content

Commit f1768c7

Browse files
araraonlineJustinZhengBC
araraonline
authored andcommitted
DOC: Adding documentation for pandas.core.indexes.api internal functions (pandas-dev#22980)
1 parent 7aed9e6 commit f1768c7

File tree

1 file changed

+115
-3
lines changed

1 file changed

+115
-3
lines changed

pandas/core/indexes/api.py

+115-3
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,28 @@
4444

4545

4646
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+
"""
5069
obs_idxes = [obj._get_axis(axis) for obj in objs
5170
if hasattr(obj, '_get_axis')]
5271
if obs_idxes:
@@ -68,6 +87,24 @@ def _get_distinct_objs(objs):
6887

6988

7089
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+
71108
# TODO: handle index names!
72109
indexes = _get_distinct_objs(indexes)
73110
if len(indexes) == 0:
@@ -91,6 +128,21 @@ def _get_combined_index(indexes, intersect=False, sort=False):
91128

92129

93130
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+
"""
94146
if len(indexes) == 0:
95147
raise AssertionError('Must have at least 1 Index to union')
96148
if len(indexes) == 1:
@@ -102,6 +154,19 @@ def _union_indexes(indexes, sort=True):
102154
indexes, kind = _sanitize_and_check(indexes)
103155

104156
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+
"""
105170
def conv(i):
106171
if isinstance(i, Index):
107172
i = i.tolist()
@@ -140,6 +205,26 @@ def conv(i):
140205

141206

142207
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+
"""
143228
kinds = list({type(index) for index in indexes})
144229

145230
if list in kinds:
@@ -158,6 +243,21 @@ def _sanitize_and_check(indexes):
158243

159244

160245
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+
"""
161261

162262
# find the non-none names, need to tupleify to make
163263
# the set hashable, then reverse on return
@@ -169,6 +269,18 @@ def _get_consensus_names(indexes):
169269

170270

171271
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+
"""
172284
first = indexes[0]
173285
for index in indexes[1:]:
174286
if not first.equals(index):

0 commit comments

Comments
 (0)