From 7266e354a1cf42c74c8c230088742a70be629c44 Mon Sep 17 00:00:00 2001 From: araraonline Date: Wed, 3 Oct 2018 20:48:12 -0300 Subject: [PATCH 01/10] Refactor _all_indexes_same --- pandas/core/indexes/api.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/pandas/core/indexes/api.py b/pandas/core/indexes/api.py index 531d8d7a3917e..68993c2a034dc 100644 --- a/pandas/core/indexes/api.py +++ b/pandas/core/indexes/api.py @@ -260,7 +260,5 @@ def _all_indexes_same(indexes): True if all indexes contain the same elements, False otherwise """ first = indexes[0] - for index in indexes[1:]: - if not first.equals(index): - return False - return True + others = indexes[1:] + return all(first.equals(other) for other in others) From 70ef4031e820d25eb4677a6aaf8511a710dddd0e Mon Sep 17 00:00:00 2001 From: araraonline Date: Wed, 3 Oct 2018 20:53:17 -0300 Subject: [PATCH 02/10] Refactor _get_consensus_names Now returns FrozenList instead of list --- pandas/core/indexes/api.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/pandas/core/indexes/api.py b/pandas/core/indexes/api.py index 68993c2a034dc..6d2ca43559089 100644 --- a/pandas/core/indexes/api.py +++ b/pandas/core/indexes/api.py @@ -7,6 +7,7 @@ ensure_index_from_sequences, InvalidIndexError) # noqa from pandas.core.indexes.category import CategoricalIndex # noqa +from pandas.core.indexes.frozen import FrozenList from pandas.core.indexes.multi import MultiIndex # noqa from pandas.core.indexes.interval import IntervalIndex # noqa from pandas.core.indexes.numeric import (NumericIndex, Float64Index, # noqa @@ -237,14 +238,11 @@ def _get_consensus_names(indexes): list A list representing the consensus 'names' found """ - - # find the non-none names, need to tupleify to make - # the set hashable, then reverse on return - consensus_names = {tuple(i.names) for i in indexes - if com._any_not_none(*i.names)} - if len(consensus_names) == 1: - return list(list(consensus_names)[0]) - return [None] * indexes[0].nlevels + non_empty = {i.names for i in indexes if com._any_not_none(*i.names)} + if len(non_empty) == 1: + return non_empty.pop() + else: + return FrozenList([None] * indexes[0].nlevels) def _all_indexes_same(indexes): From 0d2067128e43a26d944eb72bdc13362730acbfbc Mon Sep 17 00:00:00 2001 From: araraonline Date: Wed, 3 Oct 2018 21:01:23 -0300 Subject: [PATCH 03/10] Create _intersect_indexes --- pandas/core/indexes/api.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/api.py b/pandas/core/indexes/api.py index 6d2ca43559089..ed4fe7d4f988a 100644 --- a/pandas/core/indexes/api.py +++ b/pandas/core/indexes/api.py @@ -96,9 +96,7 @@ def _get_combined_index(indexes, intersect=False, sort=False): elif len(indexes) == 1: index = indexes[0] elif intersect: - index = indexes[0] - for other in indexes[1:]: - index = index.intersection(other) + index = _intersect_indexes(indexes) else: index = _union_indexes(indexes, sort=sort) index = ensure_index(index) @@ -111,6 +109,17 @@ def _get_combined_index(indexes, intersect=False, sort=False): return index +def _intersect_indexes(indexes): + """Return the intersection of indexes + + Preserves the order of the first index. + """ + index = indexes[0] + for other in indexes[1:]: + index = index.intersection(other) + return index + + def _union_indexes(indexes, sort=True): """Return the union of indexes From 13d03b97695dd80d3b279a7ddae9c2f8cc213b67 Mon Sep 17 00:00:00 2001 From: araraonline Date: Wed, 3 Oct 2018 21:14:38 -0300 Subject: [PATCH 04/10] Refactor _sanitize_and_check --- pandas/core/indexes/api.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/pandas/core/indexes/api.py b/pandas/core/indexes/api.py index ed4fe7d4f988a..2569750a06c27 100644 --- a/pandas/core/indexes/api.py +++ b/pandas/core/indexes/api.py @@ -215,18 +215,18 @@ def _sanitize_and_check(indexes): sanitized_indexes: list of Index or array-like objects type: {'list', 'array', 'special'} """ - kinds = list({type(index) for index in indexes}) + kinds = {type(index) for index in indexes} if list in kinds: - if len(kinds) > 1: - indexes = [Index(com.try_sort(x)) - if not isinstance(x, Index) else - x for x in indexes] - kinds.remove(list) - else: + if len(kinds) == 1: return indexes, 'list' + else: + indexes = [Index(com.try_sort(i)) + if not isinstance(i, Index) else i + for i in indexes] + kinds.remove(list) - if len(kinds) > 1 or Index not in kinds: + if any(kind != Index for kind in kinds): return indexes, 'special' else: return indexes, 'array' From a6e49927ab63335357e04d15578034f8f8e14143 Mon Sep 17 00:00:00 2001 From: araraonline Date: Wed, 3 Oct 2018 21:18:52 -0300 Subject: [PATCH 05/10] Refactor _unique_indices --- pandas/core/indexes/api.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pandas/core/indexes/api.py b/pandas/core/indexes/api.py index 2569750a06c27..d846ca24212af 100644 --- a/pandas/core/indexes/api.py +++ b/pandas/core/indexes/api.py @@ -163,8 +163,9 @@ def conv(i): i = i.tolist() return i - return Index( - lib.fast_unique_multiple_list([conv(i) for i in inds], sort=sort)) + inds = [conv(i) for i in inds] + ind_values = lib.fast_unique_multiple_list(inds, sort=sort) + return Index(ind_values) if kind == 'special': result = indexes[0] From 0fd7aae523293ba1568107a54ea5fa6d8ff83034 Mon Sep 17 00:00:00 2001 From: araraonline Date: Wed, 3 Oct 2018 21:24:49 -0300 Subject: [PATCH 06/10] Refactor _union_indexes (partial) --- pandas/core/indexes/api.py | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/pandas/core/indexes/api.py b/pandas/core/indexes/api.py index d846ca24212af..d845386de4714 100644 --- a/pandas/core/indexes/api.py +++ b/pandas/core/indexes/api.py @@ -135,15 +135,6 @@ def _union_indexes(indexes, sort=True): ------- Index """ - if len(indexes) == 0: - raise AssertionError('Must have at least 1 Index to union') - if len(indexes) == 1: - result = indexes[0] - if isinstance(result, list): - result = Index(sorted(result)) - return result - - indexes, kind = _sanitize_and_check(indexes) def _unique_indices(inds): """Convert indexes to lists and concatenate them, removing duplicates @@ -167,6 +158,15 @@ def conv(i): ind_values = lib.fast_unique_multiple_list(inds, sort=sort) return Index(ind_values) + if len(indexes) == 0: + raise AssertionError('Must have at least 1 Index to union') + if len(indexes) == 1: + result = indexes[0] + if isinstance(result, list): + result = Index(sorted(result)) + return result + + indexes, kind = _sanitize_and_check(indexes) if kind == 'special': result = indexes[0] From bffa8bd9a3a656c5f6ba5fdb34a266c43a8fec17 Mon Sep 17 00:00:00 2001 From: araraonline Date: Wed, 3 Oct 2018 21:56:21 -0300 Subject: [PATCH 07/10] Refactor _union_indexes (partial) --- pandas/core/indexes/api.py | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/pandas/core/indexes/api.py b/pandas/core/indexes/api.py index d845386de4714..dfdf7a804aebd 100644 --- a/pandas/core/indexes/api.py +++ b/pandas/core/indexes/api.py @@ -177,17 +177,14 @@ def conv(i): result = result.union(other) return result elif kind == 'array': - index = indexes[0] - for other in indexes[1:]: - if not index.equals(other): - - if sort is None: - # TODO: remove once pd.concat sort default changes - warnings.warn(_sort_msg, FutureWarning, stacklevel=8) - sort = True - - return _unique_indices(indexes) + if not _all_indexes_same(indexes): + if sort is None: + # TODO: remove once pd.concat sort default changes + warnings.warn(_sort_msg, FutureWarning, stacklevel=8) + sort = True + return _unique_indices(indexes) + index = indexes[0] name = _get_consensus_names(indexes)[0] if name != index.name: index = index._shallow_copy(name=name) From 844a164184785a116a74bbc74d929638761e04f3 Mon Sep 17 00:00:00 2001 From: araraonline Date: Wed, 3 Oct 2018 21:59:29 -0300 Subject: [PATCH 08/10] Refactor _union_indexes (partial) --- pandas/core/indexes/api.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pandas/core/indexes/api.py b/pandas/core/indexes/api.py index dfdf7a804aebd..a7dfa1cdcea52 100644 --- a/pandas/core/indexes/api.py +++ b/pandas/core/indexes/api.py @@ -168,15 +168,17 @@ def conv(i): indexes, kind = _sanitize_and_check(indexes) if kind == 'special': - result = indexes[0] + result = indexes[0] if hasattr(result, 'union_many'): return result.union_many(indexes[1:]) else: for other in indexes[1:]: result = result.union(other) return result + elif kind == 'array': + if not _all_indexes_same(indexes): if sort is None: # TODO: remove once pd.concat sort default changes @@ -189,6 +191,7 @@ def conv(i): if name != index.name: index = index._shallow_copy(name=name) return index + else: # kind='list' return _unique_indices(indexes) From 351e0bce77520d234439065e3afe0f7cc8eec675 Mon Sep 17 00:00:00 2001 From: araraonline Date: Wed, 3 Oct 2018 21:30:15 -0300 Subject: [PATCH 09/10] Refactor _union_indices (partial) --- pandas/core/indexes/api.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/pandas/core/indexes/api.py b/pandas/core/indexes/api.py index a7dfa1cdcea52..c035350e6a63b 100644 --- a/pandas/core/indexes/api.py +++ b/pandas/core/indexes/api.py @@ -167,6 +167,7 @@ def conv(i): return result indexes, kind = _sanitize_and_check(indexes) + if kind == 'special': result = indexes[0] @@ -179,19 +180,19 @@ def conv(i): elif kind == 'array': - if not _all_indexes_same(indexes): + if _all_indexes_same(indexes): + index = indexes[0] + name = _get_consensus_names(indexes)[0] + if name != index.name: + index = index._shallow_copy(name=name) + return index + else: if sort is None: # TODO: remove once pd.concat sort default changes warnings.warn(_sort_msg, FutureWarning, stacklevel=8) sort = True return _unique_indices(indexes) - index = indexes[0] - name = _get_consensus_names(indexes)[0] - if name != index.name: - index = index._shallow_copy(name=name) - return index - else: # kind='list' return _unique_indices(indexes) From 45b71514516f45a68ede0b45f990a2d1ba7e6896 Mon Sep 17 00:00:00 2001 From: araraonline Date: Wed, 3 Oct 2018 21:33:43 -0300 Subject: [PATCH 10/10] Refactor _union_indices (partial) --- pandas/core/indexes/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/api.py b/pandas/core/indexes/api.py index c035350e6a63b..46e6486d37c20 100644 --- a/pandas/core/indexes/api.py +++ b/pandas/core/indexes/api.py @@ -193,7 +193,7 @@ def conv(i): sort = True return _unique_indices(indexes) - else: # kind='list' + else: # kind == 'list' return _unique_indices(indexes)