@@ -307,12 +307,12 @@ def __new__(
307
307
308
308
elif (
309
309
is_datetime64_any_dtype (data )
310
- or ( dtype is not None and is_datetime64_any_dtype (dtype ) )
310
+ or is_datetime64_any_dtype (dtype )
311
311
or "tz" in kwargs
312
312
):
313
313
from pandas import DatetimeIndex
314
314
315
- if dtype is not None and is_dtype_equal (_o_dtype , dtype ):
315
+ if is_dtype_equal (_o_dtype , dtype ):
316
316
# GH#23524 passing `dtype=object` to DatetimeIndex is invalid,
317
317
# will raise in the where `data` is already tz-aware. So
318
318
# we leave it out of this step and cast to object-dtype after
@@ -327,12 +327,10 @@ def __new__(
327
327
)
328
328
return result
329
329
330
- elif is_timedelta64_dtype (data ) or (
331
- dtype is not None and is_timedelta64_dtype (dtype )
332
- ):
330
+ elif is_timedelta64_dtype (data ) or is_timedelta64_dtype (dtype ):
333
331
from pandas import TimedeltaIndex
334
332
335
- if dtype is not None and is_dtype_equal (_o_dtype , dtype ):
333
+ if is_dtype_equal (_o_dtype , dtype ):
336
334
# Note we can pass copy=False because the .astype below
337
335
# will always make a copy
338
336
result = TimedeltaIndex (data , copy = False , name = name , ** kwargs )
@@ -353,11 +351,9 @@ def __new__(
353
351
elif is_extension_array_dtype (data ) or is_extension_array_dtype (dtype ):
354
352
data = np .asarray (data )
355
353
if not (dtype is None or is_object_dtype (dtype )):
356
-
357
354
# coerce to the provided dtype
358
- data = dtype .construct_array_type ()._from_sequence (
359
- data , dtype = dtype , copy = False
360
- )
355
+ ea_cls = dtype .construct_array_type ()
356
+ data = ea_cls ._from_sequence (data , dtype = dtype , copy = False )
361
357
362
358
# coerce to the object dtype
363
359
data = data .astype (object )
@@ -366,58 +362,48 @@ def __new__(
366
362
# index-like
367
363
elif isinstance (data , (np .ndarray , Index , ABCSeries )):
368
364
if dtype is not None :
369
- try :
370
-
371
- # we need to avoid having numpy coerce
372
- # things that look like ints/floats to ints unless
373
- # they are actually ints, e.g. '0' and 0.0
374
- # should not be coerced
375
- # GH 11836
376
- if is_integer_dtype (dtype ):
377
- inferred = lib .infer_dtype (data , skipna = False )
378
- if inferred == "integer" :
379
- data = maybe_cast_to_integer_array (data , dtype , copy = copy )
380
- elif inferred in ["floating" , "mixed-integer-float" ]:
381
- if isna (data ).any ():
382
- raise ValueError ("cannot convert float NaN to integer" )
383
-
384
- if inferred == "mixed-integer-float" :
385
- data = maybe_cast_to_integer_array (data , dtype )
386
-
387
- # If we are actually all equal to integers,
388
- # then coerce to integer.
389
- try :
390
- return cls ._try_convert_to_int_index (
391
- data , copy , name , dtype
392
- )
393
- except ValueError :
394
- pass
395
-
396
- # Return an actual float index.
397
- from .numeric import Float64Index
398
-
399
- return Float64Index (data , copy = copy , dtype = dtype , name = name )
400
-
401
- elif inferred == "string" :
402
- pass
403
- else :
404
- data = data .astype (dtype )
405
- elif is_float_dtype (dtype ):
406
- inferred = lib .infer_dtype (data , skipna = False )
407
- if inferred == "string" :
365
+ # we need to avoid having numpy coerce
366
+ # things that look like ints/floats to ints unless
367
+ # they are actually ints, e.g. '0' and 0.0
368
+ # should not be coerced
369
+ # GH 11836
370
+ if is_integer_dtype (dtype ):
371
+ inferred = lib .infer_dtype (data , skipna = False )
372
+ if inferred == "integer" :
373
+ data = maybe_cast_to_integer_array (data , dtype , copy = copy )
374
+ elif inferred in ["floating" , "mixed-integer-float" ]:
375
+ if isna (data ).any ():
376
+ raise ValueError ("cannot convert float NaN to integer" )
377
+
378
+ if inferred == "mixed-integer-float" :
379
+ data = maybe_cast_to_integer_array (data , dtype )
380
+
381
+ # If we are actually all equal to integers,
382
+ # then coerce to integer.
383
+ try :
384
+ return cls ._try_convert_to_int_index (
385
+ data , copy , name , dtype
386
+ )
387
+ except ValueError :
408
388
pass
409
- else :
410
- data = data .astype (dtype )
389
+
390
+ # Return an actual float index.
391
+ from .numeric import Float64Index
392
+
393
+ return Float64Index (data , copy = copy , dtype = dtype , name = name )
394
+
395
+ elif inferred == "string" :
396
+ pass
411
397
else :
412
- data = np . array ( data , dtype = dtype , copy = copy )
413
-
414
- except ( TypeError , ValueError ) as e :
415
- msg = str ( e )
416
- if (
417
- "cannot convert float" in msg
418
- or "Trying to coerce float values to integer" in msg
419
- ) :
420
- raise
398
+ data = data . astype ( dtype )
399
+ elif is_float_dtype ( dtype ):
400
+ inferred = lib . infer_dtype ( data , skipna = False )
401
+ if inferred == "string" :
402
+ pass
403
+ else :
404
+ data = data . astype ( dtype )
405
+ else :
406
+ data = np . array ( data , dtype = dtype , copy = copy )
421
407
422
408
# maybe coerce to a sub-class
423
409
from pandas .core .indexes .period import PeriodIndex , IncompatibleFrequency
@@ -553,16 +539,6 @@ def _simple_new(cls, values, name=None, dtype=None, **kwargs):
553
539
554
540
Must be careful not to recurse.
555
541
"""
556
- if not hasattr (values , "dtype" ):
557
- if (values is None or not len (values )) and dtype is not None :
558
- values = np .empty (0 , dtype = dtype )
559
- else :
560
- values = np .array (values , copy = False )
561
- if is_object_dtype (values ):
562
- values = cls (
563
- values , name = name , dtype = dtype , ** kwargs
564
- )._ndarray_values
565
-
566
542
if isinstance (values , (ABCSeries , ABCIndexClass )):
567
543
# Index._data must always be an ndarray.
568
544
# This is no-copy for when _values is an ndarray,
@@ -1860,8 +1836,6 @@ def inferred_type(self):
1860
1836
1861
1837
@cache_readonly
1862
1838
def is_all_dates (self ):
1863
- if self ._data is None :
1864
- return False
1865
1839
return is_datetime_array (ensure_object (self .values ))
1866
1840
1867
1841
# --------------------------------------------------------------------
@@ -3132,13 +3106,9 @@ def _convert_scalar_indexer(self, key, kind=None):
3132
3106
"""
3133
3107
3134
3108
@Appender (_index_shared_docs ["_convert_slice_indexer" ])
3135
- def _convert_slice_indexer (self , key , kind = None ):
3109
+ def _convert_slice_indexer (self , key : slice , kind = None ):
3136
3110
assert kind in ["ix" , "loc" , "getitem" , "iloc" , None ]
3137
3111
3138
- # if we are not a slice, then we are done
3139
- if not isinstance (key , slice ):
3140
- return key
3141
-
3142
3112
# validate iloc
3143
3113
if kind == "iloc" :
3144
3114
return slice (
0 commit comments