@@ -757,13 +757,19 @@ def _get_join_info(self):
757
757
758
758
if self .right_index :
759
759
if len (self .left ) > 0 :
760
- join_index = self .left .index .take (left_indexer )
760
+ join_index = self ._create_join_index (self .left .index ,
761
+ self .right .index ,
762
+ left_indexer ,
763
+ how = 'right' )
761
764
else :
762
765
join_index = self .right .index .take (right_indexer )
763
766
left_indexer = np .array ([- 1 ] * len (join_index ))
764
767
elif self .left_index :
765
768
if len (self .right ) > 0 :
766
- join_index = self .right .index .take (right_indexer )
769
+ join_index = self ._create_join_index (self .right .index ,
770
+ self .left .index ,
771
+ right_indexer ,
772
+ how = 'left' )
767
773
else :
768
774
join_index = self .left .index .take (left_indexer )
769
775
right_indexer = np .array ([- 1 ] * len (join_index ))
@@ -774,6 +780,37 @@ def _get_join_info(self):
774
780
join_index = join_index .astype (object )
775
781
return join_index , left_indexer , right_indexer
776
782
783
+ def _create_join_index (self , index , other_index , indexer , how = 'left' ):
784
+ """
785
+ Create a join index by rearranging one index to match another
786
+
787
+ Parameters
788
+ ----------
789
+ index: Index being rearranged
790
+ other_index: Index used to supply values not found in index
791
+ indexer: how to rearrange index
792
+ how: replacement is only necessary if indexer based on other_index
793
+
794
+ Returns
795
+ -------
796
+ join_index
797
+ """
798
+ join_index = index .take (indexer )
799
+ if (self .how in (how , 'outer' ) and
800
+ not isinstance (other_index , MultiIndex )):
801
+ # if final index requires values in other_index but not target
802
+ # index, indexer may hold missing (-1) values, causing Index.take
803
+ # to take the final value in target index
804
+ mask = indexer == - 1
805
+ if np .any (mask ):
806
+ # if values missing (-1) from target index,
807
+ # take from other_index instead
808
+ join_list = join_index .to_numpy ()
809
+ join_list [mask ] = other_index .to_numpy ()[mask ]
810
+ join_index = Index (join_list , dtype = join_index .dtype ,
811
+ name = join_index .name )
812
+ return join_index
813
+
777
814
def _get_merge_keys (self ):
778
815
"""
779
816
Note: has side effects (copy/delete key columns)
0 commit comments