Skip to content

Commit 2f8c822

Browse files
PERF: postpone imports in Index constructor (#31423)
* PERF: postpone imports in Index constructor * added comments Co-authored-by: Tom Augspurger <[email protected]>
1 parent bb94e72 commit 2f8c822

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

pandas/core/indexes/base.py

+22-4
Original file line numberDiff line numberDiff line change
@@ -277,10 +277,6 @@ def __new__(
277277
) -> "Index":
278278

279279
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
284280

285281
name = maybe_extract_name(name, data, cls)
286282

@@ -296,10 +292,16 @@ def __new__(
296292

297293
# categorical
298294
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+
299298
return CategoricalIndex(data, dtype=dtype, copy=copy, name=name, **kwargs)
300299

301300
# interval
302301
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+
303305
closed = kwargs.pop("closed", None)
304306
if is_dtype_equal(_o_dtype, dtype):
305307
return IntervalIndex(
@@ -314,6 +316,9 @@ def __new__(
314316
or is_datetime64_any_dtype(dtype)
315317
or "tz" in kwargs
316318
):
319+
# Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423
320+
from pandas import DatetimeIndex
321+
317322
if is_dtype_equal(_o_dtype, dtype):
318323
# GH#23524 passing `dtype=object` to DatetimeIndex is invalid,
319324
# will raise in the where `data` is already tz-aware. So
@@ -328,6 +333,9 @@ def __new__(
328333
return DatetimeIndex(data, copy=copy, name=name, dtype=dtype, **kwargs)
329334

330335
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+
331339
if is_dtype_equal(_o_dtype, dtype):
332340
# Note we can pass copy=False because the .astype below
333341
# will always make a copy
@@ -338,6 +346,9 @@ def __new__(
338346
return TimedeltaIndex(data, copy=copy, name=name, dtype=dtype, **kwargs)
339347

340348
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+
341352
if is_dtype_equal(_o_dtype, dtype):
342353
return PeriodIndex(data, copy=False, name=name, **kwargs).astype(object)
343354
return PeriodIndex(data, dtype=dtype, copy=copy, name=name, **kwargs)
@@ -357,6 +368,13 @@ def __new__(
357368

358369
# index-like
359370
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+
360378
if dtype is not None:
361379
# we need to avoid having numpy coerce
362380
# things that look like ints/floats to ints unless

0 commit comments

Comments
 (0)