@@ -1002,22 +1002,24 @@ def merge(left_index, right_index):
1002
1002
return left_index .join (right_index , how = how , sort = sort )
1003
1003
1004
1004
# define condition for joining indexes
1005
- do_join_index = how is not None and any (
1006
- not indexes [0 ].equals (index ) for index in [indexes [1 :]]
1007
- )
1005
+ all_indices_equal = all (indexes [0 ].equals (index ) for index in [indexes [1 :]])
1006
+ do_join_index = how is not None and not all_indices_equal
1008
1007
1009
1008
# define condition for joining indexes with getting indexers
1010
- is_duplicates = axis == 0 and any (not index .is_unique for index in indexes )
1011
- indexers = []
1012
- if is_duplicates :
1013
- indexers = [None ] * len (indexes )
1009
+ need_indexers = (
1010
+ axis == 0
1011
+ and not all_indices_equal
1012
+ and any (not index .is_unique for index in indexes )
1013
+ )
1014
+ indexers = None
1014
1015
1015
1016
# perform joining indexes
1016
1017
if do_join_index :
1017
- if len (indexes ) == 2 and is_duplicates :
1018
+ if len (indexes ) == 2 and need_indexers :
1018
1019
# in case of count of indexes > 2 we should perform joining all indexes
1019
1020
# after that get indexers
1020
1021
# in the fast path we can obtain joined_index and indexers in one call
1022
+ indexers = [None , None ]
1021
1023
joined_index , indexers [0 ], indexers [1 ] = indexes [0 ].join (
1022
1024
indexes [1 ], how = how , sort = sort , return_indexers = True
1023
1025
)
@@ -1029,17 +1031,16 @@ def merge(left_index, right_index):
1029
1031
else :
1030
1032
joined_index = indexes [0 ].copy ()
1031
1033
1032
- if is_duplicates and indexers [0 ] is None :
1033
- for i , index in enumerate (indexes ):
1034
- indexers [i ] = index .get_indexer_for (joined_index )
1034
+ if need_indexers and indexers is None :
1035
+ indexers = [index .get_indexer_for (joined_index ) for index in indexes ]
1035
1036
1036
1037
def make_reindexer (do_reindex : bool , frame_idx : int ):
1037
1038
# the order of the frames must match the order of the indexes
1038
1039
if not do_reindex :
1039
1040
return lambda df : df
1040
1041
1041
- if is_duplicates :
1042
- assert indexers != []
1042
+ if need_indexers :
1043
+ assert indexers is not None
1043
1044
1044
1045
return lambda df : df ._reindex_with_indexers (
1045
1046
{0 : [joined_index , indexers [frame_idx ]]},
0 commit comments