@@ -4576,9 +4576,6 @@ def join(
4576
4576
pother , how = how , level = level , return_indexers = True , sort = sort
4577
4577
)
4578
4578
4579
- lindexer : np .ndarray | None
4580
- rindexer : np .ndarray | None
4581
-
4582
4579
# try to figure out the join level
4583
4580
# GH3662
4584
4581
if level is None and (self ._is_multi or other ._is_multi ):
@@ -4592,25 +4589,38 @@ def join(
4592
4589
if level is not None and (self ._is_multi or other ._is_multi ):
4593
4590
return self ._join_level (other , level , how = how )
4594
4591
4592
+ lidx : np .ndarray | None
4593
+ ridx : np .ndarray | None
4594
+
4595
4595
if len (other ) == 0 :
4596
4596
if how in ("left" , "outer" ):
4597
- join_index = self ._view ()
4598
- rindexer = np .broadcast_to (np .intp (- 1 ), len (join_index ))
4599
- return join_index , None , rindexer
4597
+ if sort and not self .is_monotonic_increasing :
4598
+ lidx = self .argsort ()
4599
+ join_index = self .take (lidx )
4600
+ else :
4601
+ lidx = None
4602
+ join_index = self ._view ()
4603
+ ridx = np .broadcast_to (np .intp (- 1 ), len (join_index ))
4604
+ return join_index , lidx , ridx
4600
4605
elif how in ("right" , "inner" , "cross" ):
4601
4606
join_index = other ._view ()
4602
- lindexer = np .array ([])
4603
- return join_index , lindexer , None
4607
+ lidx = np .array ([], dtype = np . intp )
4608
+ return join_index , lidx , None
4604
4609
4605
4610
if len (self ) == 0 :
4606
4611
if how in ("right" , "outer" ):
4607
- join_index = other ._view ()
4608
- lindexer = np .broadcast_to (np .intp (- 1 ), len (join_index ))
4609
- return join_index , lindexer , None
4612
+ if sort and not other .is_monotonic_increasing :
4613
+ ridx = other .argsort ()
4614
+ join_index = other .take (ridx )
4615
+ else :
4616
+ ridx = None
4617
+ join_index = other ._view ()
4618
+ lidx = np .broadcast_to (np .intp (- 1 ), len (join_index ))
4619
+ return join_index , lidx , ridx
4610
4620
elif how in ("left" , "inner" , "cross" ):
4611
4621
join_index = self ._view ()
4612
- rindexer = np .array ([])
4613
- return join_index , None , rindexer
4622
+ ridx = np .array ([], dtype = np . intp )
4623
+ return join_index , None , ridx
4614
4624
4615
4625
if self .dtype != other .dtype :
4616
4626
dtype = self ._find_common_type_compat (other )
0 commit comments