Skip to content
This repository was archived by the owner on Dec 22, 2019. It is now read-only.

Commit ec4363b

Browse files
author
araraonline
committed
rewrite _merge_indexes
1 parent a97924f commit ec4363b

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

pandas/core/indexes/api.py

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -278,32 +278,34 @@ def _merge_index_list(index_list,
278278
duplicate values. This is error is not raised if
279279
allow_matching_dups=True and all the indexes have a common identity.
280280
"""
281-
if verify_dups or sort:
282-
has_dups = any(ix.has_duplicates for ix in index_list)
283-
284-
if sort and has_dups:
285-
raise _CannotSortDuplicatesError("Cannot sort an index that"
286-
" contains duplicate values.")
287-
288-
if has_dups and not allow_matching_dups:
289-
raise _DuplicatesError("Index has duplicate values.")
290-
291-
uindex_list = com.get_distinct_objs(index_list) # unique index list
281+
# unique index list (a is b)
282+
uindex_list = com.get_distinct_objs(index_list)
283+
284+
# verify duplicates
285+
if sort or verify_dups:
286+
has_dups = any(ix.has_duplicates for ix in uindex_list)
287+
if has_dups:
288+
if sort:
289+
raise _CannotSortDuplicatesError("Cannot sort an index that"
290+
" contains duplicate values.")
291+
elif verify_dups and not allow_matching_dups:
292+
raise _DuplicatesError("Index has duplicate values.")
293+
elif verify_dups and allow_matching_dups and len(uindex_list) >= 2:
294+
raise _DuplicatesError("Index has duplicate values and does"
295+
" not match other indexes.")
296+
297+
# edge results
292298
if len(uindex_list) == 0:
293299
return pd.Index()
294300
elif len(uindex_list) == 1:
295301
return uindex_list[0]
296302

297-
# index_list has at least two indexes
298-
if has_dups:
299-
raise _DuplicatesError("Index has duplicate values and does"
300-
" not match other indexes.")
301-
302-
# index_list has no indexes with duplicates
303+
# reduce to one result
303304
result = uindex_list[0]
304305
for idx in uindex_list[1:]:
305306
result = _merge_indexes(result, idx)
306307

308+
# sort
307309
return result if not sort else _sort_index(result)
308310

309311

0 commit comments

Comments
 (0)