From 52cba23e26b99494e0ed3144028bc937bb1f6ce4 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Wed, 29 Jan 2020 16:49:18 +0100 Subject: [PATCH 1/2] PERF: postpone imports in Index constructor --- pandas/core/indexes/base.py | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 1738c03e754b2..48eaa41c0735e 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -277,10 +277,6 @@ def __new__( ) -> "Index": from pandas.core.indexes.range import RangeIndex - from pandas import PeriodIndex, DatetimeIndex, TimedeltaIndex - from pandas.core.indexes.numeric import Float64Index, Int64Index, UInt64Index - from pandas.core.indexes.interval import IntervalIndex - from pandas.core.indexes.category import CategoricalIndex name = maybe_extract_name(name, data, cls) @@ -296,10 +292,14 @@ def __new__( # categorical elif is_categorical_dtype(data) or is_categorical_dtype(dtype): + from pandas.core.indexes.category import CategoricalIndex + return CategoricalIndex(data, dtype=dtype, copy=copy, name=name, **kwargs) # interval elif is_interval_dtype(data) or is_interval_dtype(dtype): + from pandas.core.indexes.interval import IntervalIndex + closed = kwargs.pop("closed", None) if is_dtype_equal(_o_dtype, dtype): return IntervalIndex( @@ -314,6 +314,8 @@ def __new__( or is_datetime64_any_dtype(dtype) or "tz" in kwargs ): + from pandas import DatetimeIndex + if is_dtype_equal(_o_dtype, dtype): # GH#23524 passing `dtype=object` to DatetimeIndex is invalid, # will raise in the where `data` is already tz-aware. So @@ -328,6 +330,8 @@ def __new__( return DatetimeIndex(data, copy=copy, name=name, dtype=dtype, **kwargs) elif is_timedelta64_dtype(data) or is_timedelta64_dtype(dtype): + from pandas import TimedeltaIndex + if is_dtype_equal(_o_dtype, dtype): # Note we can pass copy=False because the .astype below # will always make a copy @@ -338,6 +342,8 @@ def __new__( return TimedeltaIndex(data, copy=copy, name=name, dtype=dtype, **kwargs) elif is_period_dtype(data) or is_period_dtype(dtype): + from pandas import PeriodIndex + if is_dtype_equal(_o_dtype, dtype): return PeriodIndex(data, copy=False, name=name, **kwargs).astype(object) return PeriodIndex(data, dtype=dtype, copy=copy, name=name, **kwargs) @@ -357,6 +363,12 @@ def __new__( # index-like elif isinstance(data, (np.ndarray, Index, ABCSeries)): + from pandas.core.indexes.numeric import ( + Float64Index, + Int64Index, + UInt64Index, + ) + if dtype is not None: # we need to avoid having numpy coerce # things that look like ints/floats to ints unless From dd6ee1b47231f476c5cdd3a9e54f9e733b160653 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Wed, 29 Jan 2020 10:57:01 -0600 Subject: [PATCH 2/2] added comments --- pandas/core/indexes/base.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 48eaa41c0735e..5274213f114e3 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -292,12 +292,14 @@ def __new__( # categorical elif is_categorical_dtype(data) or is_categorical_dtype(dtype): + # Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423 from pandas.core.indexes.category import CategoricalIndex return CategoricalIndex(data, dtype=dtype, copy=copy, name=name, **kwargs) # interval elif is_interval_dtype(data) or is_interval_dtype(dtype): + # Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423 from pandas.core.indexes.interval import IntervalIndex closed = kwargs.pop("closed", None) @@ -314,6 +316,7 @@ def __new__( or is_datetime64_any_dtype(dtype) or "tz" in kwargs ): + # Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423 from pandas import DatetimeIndex if is_dtype_equal(_o_dtype, dtype): @@ -330,6 +333,7 @@ def __new__( return DatetimeIndex(data, copy=copy, name=name, dtype=dtype, **kwargs) elif is_timedelta64_dtype(data) or is_timedelta64_dtype(dtype): + # Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423 from pandas import TimedeltaIndex if is_dtype_equal(_o_dtype, dtype): @@ -342,6 +346,7 @@ def __new__( return TimedeltaIndex(data, copy=copy, name=name, dtype=dtype, **kwargs) elif is_period_dtype(data) or is_period_dtype(dtype): + # Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423 from pandas import PeriodIndex if is_dtype_equal(_o_dtype, dtype): @@ -363,6 +368,7 @@ def __new__( # index-like elif isinstance(data, (np.ndarray, Index, ABCSeries)): + # Delay import for perf. https://github.com/pandas-dev/pandas/pull/31423 from pandas.core.indexes.numeric import ( Float64Index, Int64Index,