Skip to content

Commit 63b2e53

Browse files
jorisvandenbosschemeeseeksmachine
authored andcommitted
Backport PR pandas-dev#31423: PERF: postpone imports in Index constructor
1 parent c62e6c0 commit 63b2e53

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
@@ -278,10 +278,6 @@ def __new__(
278278
) -> "Index":
279279

280280
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
285281

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

@@ -297,10 +293,16 @@ def __new__(
297293

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

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

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

341349
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+
342353
if is_dtype_equal(_o_dtype, dtype):
343354
return PeriodIndex(data, copy=False, name=name, **kwargs).astype(object)
344355
return PeriodIndex(data, dtype=dtype, copy=copy, name=name, **kwargs)
@@ -358,6 +369,13 @@ def __new__(
358369

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

0 commit comments

Comments
 (0)