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