@@ -307,12 +307,12 @@ def __new__(
307307
308308 elif (
309309 is_datetime64_any_dtype (data )
310- or ( dtype is not None and is_datetime64_any_dtype (dtype ) )
310+ or is_datetime64_any_dtype (dtype )
311311 or "tz" in kwargs
312312 ):
313313 from pandas import DatetimeIndex
314314
315- if dtype is not None and is_dtype_equal (_o_dtype , dtype ):
315+ if is_dtype_equal (_o_dtype , dtype ):
316316 # GH#23524 passing `dtype=object` to DatetimeIndex is invalid,
317317 # will raise in the where `data` is already tz-aware. So
318318 # we leave it out of this step and cast to object-dtype after
@@ -327,12 +327,10 @@ def __new__(
327327 )
328328 return result
329329
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 ):
333331 from pandas import TimedeltaIndex
334332
335- if dtype is not None and is_dtype_equal (_o_dtype , dtype ):
333+ if is_dtype_equal (_o_dtype , dtype ):
336334 # Note we can pass copy=False because the .astype below
337335 # will always make a copy
338336 result = TimedeltaIndex (data , copy = False , name = name , ** kwargs )
@@ -353,11 +351,9 @@ def __new__(
353351 elif is_extension_array_dtype (data ) or is_extension_array_dtype (dtype ):
354352 data = np .asarray (data )
355353 if not (dtype is None or is_object_dtype (dtype )):
356-
357354 # 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 )
361357
362358 # coerce to the object dtype
363359 data = data .astype (object )
@@ -366,58 +362,48 @@ def __new__(
366362 # index-like
367363 elif isinstance (data , (np .ndarray , Index , ABCSeries )):
368364 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 :
408388 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
411397 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 )
421407
422408 # maybe coerce to a sub-class
423409 from pandas .core .indexes .period import PeriodIndex , IncompatibleFrequency
@@ -553,16 +539,6 @@ def _simple_new(cls, values, name=None, dtype=None, **kwargs):
553539
554540 Must be careful not to recurse.
555541 """
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-
566542 if isinstance (values , (ABCSeries , ABCIndexClass )):
567543 # Index._data must always be an ndarray.
568544 # This is no-copy for when _values is an ndarray,
@@ -1860,8 +1836,6 @@ def inferred_type(self):
18601836
18611837 @cache_readonly
18621838 def is_all_dates (self ):
1863- if self ._data is None :
1864- return False
18651839 return is_datetime_array (ensure_object (self .values ))
18661840
18671841 # --------------------------------------------------------------------
@@ -3132,13 +3106,9 @@ def _convert_scalar_indexer(self, key, kind=None):
31323106 """
31333107
31343108 @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 ):
31363110 assert kind in ["ix" , "loc" , "getitem" , "iloc" , None ]
31373111
3138- # if we are not a slice, then we are done
3139- if not isinstance (key , slice ):
3140- return key
3141-
31423112 # validate iloc
31433113 if kind == "iloc" :
31443114 return slice (
0 commit comments