10
10
import pandas ._libs .join as libjoin
11
11
from pandas ._libs .lib import is_datetime_array
12
12
from pandas ._libs .tslibs import OutOfBoundsDatetime , Timestamp
13
+ from pandas ._libs .tslibs .period import IncompatibleFrequency
13
14
from pandas ._libs .tslibs .timezones import tz_compare
14
15
from pandas .compat import set_function_name
15
16
from pandas .compat .numpy import function as nv
@@ -262,7 +263,13 @@ def __new__(
262
263
fastpath = None ,
263
264
tupleize_cols = True ,
264
265
** kwargs
265
- ):
266
+ ) -> "Index" :
267
+
268
+ from .range import RangeIndex
269
+ from pandas import PeriodIndex , DatetimeIndex , TimedeltaIndex
270
+ from .numeric import Float64Index , Int64Index , UInt64Index
271
+ from .interval import IntervalIndex
272
+ from .category import CategoricalIndex
266
273
267
274
if name is None and hasattr (data , "name" ):
268
275
name = data .name
@@ -277,8 +284,6 @@ def __new__(
277
284
if fastpath :
278
285
return cls ._simple_new (data , name )
279
286
280
- from .range import RangeIndex
281
-
282
287
if isinstance (data , ABCPandasArray ):
283
288
# ensure users don't accidentally put a PandasArray in an index.
284
289
data = data .to_numpy ()
@@ -291,16 +296,12 @@ def __new__(
291
296
292
297
# categorical
293
298
elif is_categorical_dtype (data ) or is_categorical_dtype (dtype ):
294
- from .category import CategoricalIndex
295
-
296
299
return CategoricalIndex (data , dtype = dtype , copy = copy , name = name , ** kwargs )
297
300
298
301
# interval
299
302
elif (
300
303
is_interval_dtype (data ) or is_interval_dtype (dtype )
301
304
) and not is_object_dtype (dtype ):
302
- from .interval import IntervalIndex
303
-
304
305
closed = kwargs .get ("closed" , None )
305
306
return IntervalIndex (data , dtype = dtype , name = name , copy = copy , closed = closed )
306
307
@@ -309,42 +310,31 @@ def __new__(
309
310
or is_datetime64_any_dtype (dtype )
310
311
or "tz" in kwargs
311
312
):
312
- from pandas import DatetimeIndex
313
-
314
313
if is_dtype_equal (_o_dtype , dtype ):
315
314
# GH#23524 passing `dtype=object` to DatetimeIndex is invalid,
316
315
# will raise in the where `data` is already tz-aware. So
317
316
# we leave it out of this step and cast to object-dtype after
318
317
# the DatetimeIndex construction.
319
318
# Note we can pass copy=False because the .astype below
320
319
# will always make a copy
321
- result = DatetimeIndex (data , copy = False , name = name , ** kwargs )
320
+ result = DatetimeIndex (
321
+ data , copy = False , name = name , ** kwargs
322
+ ) # type: "Index"
322
323
return result .astype (object )
323
324
else :
324
- result = DatetimeIndex (
325
- data , copy = copy , name = name , dtype = dtype , ** kwargs
326
- )
327
- return result
325
+ return DatetimeIndex (data , copy = copy , name = name , dtype = dtype , ** kwargs )
328
326
329
327
elif is_timedelta64_dtype (data ) or is_timedelta64_dtype (dtype ):
330
- from pandas import TimedeltaIndex
331
-
332
328
if is_dtype_equal (_o_dtype , dtype ):
333
329
# Note we can pass copy=False because the .astype below
334
330
# will always make a copy
335
331
result = TimedeltaIndex (data , copy = False , name = name , ** kwargs )
336
332
return result .astype (object )
337
333
else :
338
- result = TimedeltaIndex (
339
- data , copy = copy , name = name , dtype = dtype , ** kwargs
340
- )
341
- return result
334
+ return TimedeltaIndex (data , copy = copy , name = name , dtype = dtype , ** kwargs )
342
335
343
336
elif is_period_dtype (data ) and not is_object_dtype (dtype ):
344
- from pandas import PeriodIndex
345
-
346
- result = PeriodIndex (data , copy = copy , name = name , ** kwargs )
347
- return result
337
+ return PeriodIndex (data , copy = copy , name = name , ** kwargs )
348
338
349
339
# extension dtype
350
340
elif is_extension_array_dtype (data ) or is_extension_array_dtype (dtype ):
@@ -387,8 +377,6 @@ def __new__(
387
377
pass
388
378
389
379
# Return an actual float index.
390
- from .numeric import Float64Index
391
-
392
380
return Float64Index (data , copy = copy , dtype = dtype , name = name )
393
381
394
382
elif inferred == "string" :
@@ -405,19 +393,11 @@ def __new__(
405
393
data = np .array (data , dtype = dtype , copy = copy )
406
394
407
395
# maybe coerce to a sub-class
408
- from pandas .core .indexes .period import PeriodIndex , IncompatibleFrequency
409
-
410
396
if is_signed_integer_dtype (data .dtype ):
411
- from .numeric import Int64Index
412
-
413
397
return Int64Index (data , copy = copy , dtype = dtype , name = name )
414
398
elif is_unsigned_integer_dtype (data .dtype ):
415
- from .numeric import UInt64Index
416
-
417
399
return UInt64Index (data , copy = copy , dtype = dtype , name = name )
418
400
elif is_float_dtype (data .dtype ):
419
- from .numeric import Float64Index
420
-
421
401
return Float64Index (data , copy = copy , dtype = dtype , name = name )
422
402
elif issubclass (data .dtype .type , np .bool ) or is_bool_dtype (data ):
423
403
subarr = data .astype ("object" )
@@ -440,12 +420,8 @@ def __new__(
440
420
return Index (subarr , copy = copy , dtype = object , name = name )
441
421
elif inferred in ["floating" , "mixed-integer-float" , "integer-na" ]:
442
422
# TODO: Returns IntegerArray for integer-na case in the future
443
- from .numeric import Float64Index
444
-
445
423
return Float64Index (subarr , copy = copy , name = name )
446
424
elif inferred == "interval" :
447
- from .interval import IntervalIndex
448
-
449
425
try :
450
426
return IntervalIndex (subarr , name = name , copy = copy )
451
427
except ValueError :
@@ -456,8 +432,6 @@ def __new__(
456
432
pass
457
433
elif inferred != "string" :
458
434
if inferred .startswith ("datetime" ):
459
- from pandas import DatetimeIndex
460
-
461
435
try :
462
436
return DatetimeIndex (subarr , copy = copy , name = name , ** kwargs )
463
437
except (ValueError , OutOfBoundsDatetime ):
@@ -467,8 +441,6 @@ def __new__(
467
441
pass
468
442
469
443
elif inferred .startswith ("timedelta" ):
470
- from pandas import TimedeltaIndex
471
-
472
444
return TimedeltaIndex (subarr , copy = copy , name = name , ** kwargs )
473
445
elif inferred == "period" :
474
446
try :
0 commit comments