@@ -209,60 +209,6 @@ def union_indexes(indexes, sort: bool | None = True) -> Index:
209
209
210
210
indexes , kind = _sanitize_and_check (indexes )
211
211
212
- def _unique_indices (inds , dtype ) -> Index :
213
- """
214
- Concatenate indices and remove duplicates.
215
-
216
- Parameters
217
- ----------
218
- inds : list of Index or list objects
219
- dtype : dtype to set for the resulting Index
220
-
221
- Returns
222
- -------
223
- Index
224
- """
225
- if all (isinstance (ind , Index ) for ind in inds ):
226
- inds = [ind .astype (dtype , copy = False ) for ind in inds ]
227
- result = inds [0 ].unique ()
228
- other = inds [1 ].append (inds [2 :])
229
- diff = other [result .get_indexer_for (other ) == - 1 ]
230
- if len (diff ):
231
- result = result .append (diff .unique ())
232
- if sort :
233
- result = result .sort_values ()
234
- return result
235
-
236
- def conv (i ):
237
- if isinstance (i , Index ):
238
- i = i .tolist ()
239
- return i
240
-
241
- return Index (
242
- lib .fast_unique_multiple_list ([conv (i ) for i in inds ], sort = sort ),
243
- dtype = dtype ,
244
- )
245
-
246
- def _find_common_index_dtype (inds ):
247
- """
248
- Finds a common type for the indexes to pass through to resulting index.
249
-
250
- Parameters
251
- ----------
252
- inds: list of Index or list objects
253
-
254
- Returns
255
- -------
256
- The common type or None if no indexes were given
257
- """
258
- dtypes = [idx .dtype for idx in indexes if isinstance (idx , Index )]
259
- if dtypes :
260
- dtype = find_common_type (dtypes )
261
- else :
262
- dtype = None
263
-
264
- return dtype
265
-
266
212
if kind == "special" :
267
213
result = indexes [0 ]
268
214
@@ -294,18 +240,36 @@ def _find_common_index_dtype(inds):
294
240
return result
295
241
296
242
elif kind == "array" :
297
- dtype = _find_common_index_dtype (indexes )
298
- index = indexes [0 ]
299
- if not all (index .equals (other ) for other in indexes [1 :]):
300
- index = _unique_indices (indexes , dtype )
243
+ if not all_indexes_same (indexes ):
244
+ dtype = find_common_type ([idx .dtype for idx in indexes ])
245
+ inds = [ind .astype (dtype , copy = False ) for ind in indexes ]
246
+ index = inds [0 ].unique ()
247
+ other = inds [1 ].append (inds [2 :])
248
+ diff = other [index .get_indexer_for (other ) == - 1 ]
249
+ if len (diff ):
250
+ index = index .append (diff .unique ())
251
+ if sort :
252
+ index = index .sort_values ()
253
+ else :
254
+ index = indexes [0 ]
301
255
302
256
name = get_unanimous_names (* indexes )[0 ]
303
257
if name != index .name :
304
258
index = index .rename (name )
305
259
return index
306
- else : # kind='list'
307
- dtype = _find_common_index_dtype (indexes )
308
- return _unique_indices (indexes , dtype )
260
+ elif kind == "list" :
261
+ dtypes = [idx .dtype for idx in indexes if isinstance (idx , Index )]
262
+ if dtypes :
263
+ dtype = find_common_type (dtypes )
264
+ else :
265
+ dtype = None
266
+ all_lists = (idx .tolist () if isinstance (idx , Index ) else idx for idx in indexes )
267
+ return Index (
268
+ lib .fast_unique_multiple_list_gen (all_lists , sort = bool (sort )),
269
+ dtype = dtype ,
270
+ )
271
+ else :
272
+ raise ValueError (f"{ kind = } must be 'special', 'array' or 'list'." )
309
273
310
274
311
275
def _sanitize_and_check (indexes ):
@@ -329,14 +293,14 @@ def _sanitize_and_check(indexes):
329
293
sanitized_indexes : list of Index or list objects
330
294
type : {'list', 'array', 'special'}
331
295
"""
332
- kinds = list ( {type (index ) for index in indexes })
296
+ kinds = {type (index ) for index in indexes }
333
297
334
298
if list in kinds :
335
299
if len (kinds ) > 1 :
336
300
indexes = [
337
301
Index (list (x )) if not isinstance (x , Index ) else x for x in indexes
338
302
]
339
- kinds . remove ( list )
303
+ kinds -= { list }
340
304
else :
341
305
return indexes , "list"
342
306
0 commit comments