@@ -4576,9 +4576,6 @@ def join(
45764576 pother , how = how , level = level , return_indexers = True , sort = sort
45774577 )
45784578
4579- lindexer : np .ndarray | None
4580- rindexer : np .ndarray | None
4581-
45824579 # try to figure out the join level
45834580 # GH3662
45844581 if level is None and (self ._is_multi or other ._is_multi ):
@@ -4592,25 +4589,38 @@ def join(
45924589 if level is not None and (self ._is_multi or other ._is_multi ):
45934590 return self ._join_level (other , level , how = how )
45944591
4592+ lidx : np .ndarray | None
4593+ ridx : np .ndarray | None
4594+
45954595 if len (other ) == 0 :
45964596 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
46004605 elif how in ("right" , "inner" , "cross" ):
46014606 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
46044609
46054610 if len (self ) == 0 :
46064611 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
46104620 elif how in ("left" , "inner" , "cross" ):
46114621 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
46144624
46154625 if self .dtype != other .dtype :
46164626 dtype = self ._find_common_type_compat (other )
0 commit comments