6
6
TYPE_CHECKING ,
7
7
Any ,
8
8
Callable ,
9
+ Dict ,
9
10
FrozenSet ,
10
11
Hashable ,
11
12
List ,
131
132
_Identity = NewType ("_Identity" , object )
132
133
133
134
135
+ def disallow_kwargs (kwargs : Dict [str , Any ]):
136
+ if kwargs :
137
+ raise TypeError (f"Unexpected keyword arguments { repr (set (kwargs ))} " )
138
+
139
+
134
140
def _new_Index (cls , d ):
135
141
"""
136
142
This is called upon unpickling, rather than the default which doesn't
@@ -296,32 +302,29 @@ def __new__(
296
302
return result .astype (dtype , copy = False )
297
303
return result
298
304
299
- if is_ea_or_datetimelike_dtype (dtype ):
305
+ elif is_ea_or_datetimelike_dtype (dtype ):
300
306
# non-EA dtype indexes have special casting logic, so we punt here
301
307
klass = cls ._dtype_to_subclass (dtype )
302
308
if klass is not Index :
303
309
return klass (data , dtype = dtype , copy = copy , name = name , ** kwargs )
304
310
305
- if is_ea_or_datetimelike_dtype (data_dtype ):
311
+ ea_cls = dtype .construct_array_type ()
312
+ data = ea_cls ._from_sequence (data , dtype = dtype , copy = copy )
313
+ data = np .asarray (data , dtype = object )
314
+ disallow_kwargs (kwargs )
315
+ return Index ._simple_new (data , name = name )
316
+
317
+ elif is_ea_or_datetimelike_dtype (data_dtype ):
306
318
klass = cls ._dtype_to_subclass (data_dtype )
307
319
if klass is not Index :
308
320
result = klass (data , copy = copy , name = name , ** kwargs )
309
321
if dtype is not None :
310
322
return result .astype (dtype , copy = False )
311
323
return result
312
324
313
- # extension dtype
314
- if is_extension_array_dtype (data_dtype ) or is_extension_array_dtype (dtype ):
315
- if not (dtype is None or is_object_dtype (dtype )):
316
- # coerce to the provided dtype
317
- ea_cls = dtype .construct_array_type ()
318
- data = ea_cls ._from_sequence (data , dtype = dtype , copy = False )
319
- else :
320
- data = np .asarray (data , dtype = object )
321
-
322
- # coerce to the object dtype
323
- data = data .astype (object )
324
- return Index (data , dtype = object , copy = copy , name = name , ** kwargs )
325
+ data = np .array (data , dtype = object , copy = copy )
326
+ disallow_kwargs (kwargs )
327
+ return Index ._simple_new (data , name = name )
325
328
326
329
# index-like
327
330
elif isinstance (data , (np .ndarray , Index , ABCSeries )):
@@ -333,7 +336,7 @@ def __new__(
333
336
# should not be coerced
334
337
# GH 11836
335
338
data = _maybe_cast_with_dtype (data , dtype , copy )
336
- dtype = data .dtype # TODO: maybe not for object?
339
+ dtype = data .dtype
337
340
338
341
if data .dtype .kind in ["i" , "u" , "f" ]:
339
342
# maybe coerce to a sub-class
@@ -342,16 +345,15 @@ def __new__(
342
345
arr = com .asarray_tuplesafe (data , dtype = object )
343
346
344
347
if dtype is None :
345
- new_data = _maybe_cast_data_without_dtype (arr )
346
- new_dtype = new_data .dtype
347
- return cls (
348
- new_data , dtype = new_dtype , copy = copy , name = name , ** kwargs
349
- )
348
+ arr = _maybe_cast_data_without_dtype (arr )
349
+ dtype = arr .dtype
350
+
351
+ if kwargs :
352
+ return cls ( arr , dtype , copy = copy , name = name , ** kwargs )
350
353
351
354
klass = cls ._dtype_to_subclass (arr .dtype )
352
355
arr = klass ._ensure_array (arr , dtype , copy )
353
- if kwargs :
354
- raise TypeError (f"Unexpected keyword arguments { repr (set (kwargs ))} " )
356
+ disallow_kwargs (kwargs )
355
357
return klass ._simple_new (arr , name )
356
358
357
359
elif is_scalar (data ):
0 commit comments