@@ -2183,7 +2183,7 @@ def _get_consensus_name(self, other):
2183
2183
return self ._shallow_copy (name = name )
2184
2184
return self
2185
2185
2186
- def union (self , other ):
2186
+ def union (self , other , sort = True ):
2187
2187
"""
2188
2188
Form the union of two Index objects and sorts if possible.
2189
2189
@@ -2241,27 +2241,29 @@ def union(self, other):
2241
2241
allow_fill = False )
2242
2242
result = _concat ._concat_compat ((self ._values , other_diff ))
2243
2243
2244
- try :
2245
- self ._values [0 ] < other_diff [0 ]
2246
- except TypeError as e :
2247
- warnings .warn ("%s, sort order is undefined for "
2248
- "incomparable objects" % e , RuntimeWarning ,
2249
- stacklevel = 3 )
2250
- else :
2251
- types = frozenset ((self .inferred_type ,
2252
- other .inferred_type ))
2253
- if not types & _unsortable_types :
2254
- result .sort ()
2244
+ if sort :
2245
+ try :
2246
+ self ._values [0 ] < other_diff [0 ]
2247
+ except TypeError as e :
2248
+ warnings .warn ("%s, sort order is undefined for "
2249
+ "incomparable objects" % e , RuntimeWarning ,
2250
+ stacklevel = 3 )
2251
+ else :
2252
+ types = frozenset ((self .inferred_type ,
2253
+ other .inferred_type ))
2254
+ if not types & _unsortable_types :
2255
+ result .sort ()
2255
2256
2256
2257
else :
2257
2258
result = self ._values
2258
2259
2259
- try :
2260
- result = np .sort (result )
2261
- except TypeError as e :
2262
- warnings .warn ("%s, sort order is undefined for "
2263
- "incomparable objects" % e , RuntimeWarning ,
2264
- stacklevel = 3 )
2260
+ if sort :
2261
+ try :
2262
+ result = np .sort (result )
2263
+ except TypeError as e :
2264
+ warnings .warn ("%s, sort order is undefined for "
2265
+ "incomparable objects" % e , RuntimeWarning ,
2266
+ stacklevel = 3 )
2265
2267
2266
2268
# for subclasses
2267
2269
return self ._wrap_union_result (other , result )
0 commit comments