@@ -278,10 +278,6 @@ def __new__(
278
278
) -> "Index" :
279
279
280
280
from pandas .core .indexes .range import RangeIndex
281
- from pandas import PeriodIndex , DatetimeIndex , TimedeltaIndex
282
- from pandas .core .indexes .numeric import Float64Index , Int64Index , UInt64Index
283
- from pandas .core .indexes .interval import IntervalIndex
284
- from pandas .core .indexes .category import CategoricalIndex
285
281
286
282
name = maybe_extract_name (name , data , cls )
287
283
@@ -297,10 +293,16 @@ def __new__(
297
293
298
294
# categorical
299
295
elif is_categorical_dtype (data ) or is_categorical_dtype (dtype ):
296
+ # Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
297
+ from pandas .core .indexes .category import CategoricalIndex
298
+
300
299
return CategoricalIndex (data , dtype = dtype , copy = copy , name = name , ** kwargs )
301
300
302
301
# interval
303
302
elif is_interval_dtype (data ) or is_interval_dtype (dtype ):
303
+ # Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
304
+ from pandas .core .indexes .interval import IntervalIndex
305
+
304
306
closed = kwargs .pop ("closed" , None )
305
307
if is_dtype_equal (_o_dtype , dtype ):
306
308
return IntervalIndex (
@@ -315,6 +317,9 @@ def __new__(
315
317
or is_datetime64_any_dtype (dtype )
316
318
or "tz" in kwargs
317
319
):
320
+ # Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
321
+ from pandas import DatetimeIndex
322
+
318
323
if is_dtype_equal (_o_dtype , dtype ):
319
324
# GH#23524 passing `dtype=object` to DatetimeIndex is invalid,
320
325
# will raise in the where `data` is already tz-aware. So
@@ -329,6 +334,9 @@ def __new__(
329
334
return DatetimeIndex (data , copy = copy , name = name , dtype = dtype , ** kwargs )
330
335
331
336
elif is_timedelta64_dtype (data ) or is_timedelta64_dtype (dtype ):
337
+ # Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
338
+ from pandas import TimedeltaIndex
339
+
332
340
if is_dtype_equal (_o_dtype , dtype ):
333
341
# Note we can pass copy=False because the .astype below
334
342
# will always make a copy
@@ -339,6 +347,9 @@ def __new__(
339
347
return TimedeltaIndex (data , copy = copy , name = name , dtype = dtype , ** kwargs )
340
348
341
349
elif is_period_dtype (data ) or is_period_dtype (dtype ):
350
+ # Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
351
+ from pandas import PeriodIndex
352
+
342
353
if is_dtype_equal (_o_dtype , dtype ):
343
354
return PeriodIndex (data , copy = False , name = name , ** kwargs ).astype (object )
344
355
return PeriodIndex (data , dtype = dtype , copy = copy , name = name , ** kwargs )
@@ -358,6 +369,13 @@ def __new__(
358
369
359
370
# index-like
360
371
elif isinstance (data , (np .ndarray , Index , ABCSeries )):
372
+ # Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
373
+ from pandas .core .indexes .numeric import (
374
+ Float64Index ,
375
+ Int64Index ,
376
+ UInt64Index ,
377
+ )
378
+
361
379
if dtype is not None :
362
380
# we need to avoid having numpy coerce
363
381
# things that look like ints/floats to ints unless
0 commit comments