@@ -209,60 +209,6 @@ def union_indexes(indexes, sort: bool | None = True) -> Index:
209209
210210 indexes , kind = _sanitize_and_check (indexes )
211211
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-
266212 if kind == "special" :
267213 result = indexes [0 ]
268214
@@ -294,18 +240,36 @@ def _find_common_index_dtype(inds):
294240 return result
295241
296242 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 ]
301255
302256 name = get_unanimous_names (* indexes )[0 ]
303257 if name != index .name :
304258 index = index .rename (name )
305259 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'." )
309273
310274
311275def _sanitize_and_check (indexes ):
@@ -329,14 +293,14 @@ def _sanitize_and_check(indexes):
329293 sanitized_indexes : list of Index or list objects
330294 type : {'list', 'array', 'special'}
331295 """
332- kinds = list ( {type (index ) for index in indexes })
296+ kinds = {type (index ) for index in indexes }
333297
334298 if list in kinds :
335299 if len (kinds ) > 1 :
336300 indexes = [
337301 Index (list (x )) if not isinstance (x , Index ) else x for x in indexes
338302 ]
339- kinds . remove ( list )
303+ kinds -= { list }
340304 else :
341305 return indexes , "list"
342306
0 commit comments