From e6cedca6ec2058a9eb08e1b23073119830444ad6 Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 15 Nov 2020 10:30:43 -0800 Subject: [PATCH 1/4] CLN: remove unnecessary no_setting_name setting --- pandas/core/indexes/category.py | 1 - pandas/core/indexes/datetimes.py | 2 +- pandas/core/indexes/interval.py | 1 - 3 files changed, 1 insertion(+), 3 deletions(-) diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index 06df8f85cded7..4b79ac9faf769 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -224,7 +224,6 @@ def _simple_new(cls, values: Categorical, name: Label = None): result._cache = {} result._reset_identity() - result._no_setting_name = False return result # -------------------------------------------------------------------- diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 9744eb0ecbb88..5d0ecf8f8fe6c 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -327,7 +327,7 @@ def _simple_new(cls, values: DatetimeArray, name: Label = None): result._data = values result.name = name result._cache = {} - result._no_setting_name = False + # For groupby perf. See note in indexes/base about _index_data result._index_data = values._data result._reset_identity() diff --git a/pandas/core/indexes/interval.py b/pandas/core/indexes/interval.py index 2aec86c9cdfae..bfbb4a00638fc 100644 --- a/pandas/core/indexes/interval.py +++ b/pandas/core/indexes/interval.py @@ -239,7 +239,6 @@ def _simple_new(cls, array: IntervalArray, name: Label = None): result._data = array result.name = name result._cache = {} - result._no_setting_name = False result._reset_identity() return result From ed5a445bec44916b4bfd59592e06c69f0e154a8a Mon Sep 17 00:00:00 2001 From: Brock Date: Sun, 15 Nov 2020 12:20:41 -0800 Subject: [PATCH 2/4] REF: share _simple_new --- pandas/core/indexes/base.py | 2 +- pandas/core/indexes/datetimelike.py | 15 +++++++++++++++ pandas/core/indexes/datetimes.py | 16 +--------------- pandas/core/indexes/period.py | 22 ---------------------- pandas/core/indexes/timedeltas.py | 16 +--------------- 5 files changed, 18 insertions(+), 53 deletions(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index cb5641a74e60b..2e5be63c4bd26 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -418,7 +418,7 @@ def asi8(self): return None @classmethod - def _simple_new(cls, values, name: Label = None): + def _simple_new(cls, values: np.ndarray, name: Label = None): """ We require that we have a dtype compat for the values. If we are passed a non-dtype compat, then coerce using the constructor. diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index 40a6086f69f85..cb76bf43e4184 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -100,6 +100,21 @@ class DatetimeIndexOpsMixin(NDArrayBackedExtensionIndex): ) _hasnans = hasnans # for index / array -agnostic code + @classmethod + def _simple_new(cls, values, name: Label = None): + assert isinstance(values, cls.__annotations__["_data"]), type(values) + + result = object.__new__(cls) + result._data = values + result._name = name + result._cache = {} + + # For groupby perf. See note in indexes/base about _index_data + result._index_data = values._data + + result._reset_identity() + return result + @property def _is_all_dates(self) -> bool: return True diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 5d0ecf8f8fe6c..d84ae4d0803d6 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -14,7 +14,7 @@ to_offset, ) from pandas._libs.tslibs.offsets import prefix_mapping -from pandas._typing import DtypeObj, Label +from pandas._typing import DtypeObj from pandas.errors import InvalidIndexError from pandas.util._decorators import cache_readonly, doc @@ -319,20 +319,6 @@ def __new__( subarr = cls._simple_new(dtarr, name=name) return subarr - @classmethod - def _simple_new(cls, values: DatetimeArray, name: Label = None): - assert isinstance(values, DatetimeArray), type(values) - - result = object.__new__(cls) - result._data = values - result.name = name - result._cache = {} - - # For groupby perf. See note in indexes/base about _index_data - result._index_data = values._data - result._reset_identity() - return result - # -------------------------------------------------------------------- @cache_readonly diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 44c20ad0de848..16ed01f4a75d1 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -244,28 +244,6 @@ def __new__( return cls._simple_new(data, name=name) - @classmethod - def _simple_new(cls, values: PeriodArray, name: Label = None): - """ - Create a new PeriodIndex. - - Parameters - ---------- - values : PeriodArray - Values that can be converted to a PeriodArray without inference - or coercion. - """ - assert isinstance(values, PeriodArray), type(values) - - result = object.__new__(cls) - result._data = values - # For groupby perf. See note in indexes/base about _index_data - result._index_data = values._data - result.name = name - result._cache = {} - result._reset_identity() - return result - # ------------------------------------------------------------------------ # Data diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index cf5fa4bbb3d75..b42d401c027be 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -2,7 +2,7 @@ from pandas._libs import index as libindex, lib from pandas._libs.tslibs import Timedelta, to_offset -from pandas._typing import DtypeObj, Label +from pandas._typing import DtypeObj from pandas.errors import InvalidIndexError from pandas.util._decorators import doc @@ -156,20 +156,6 @@ def __new__( ) return cls._simple_new(tdarr, name=name) - @classmethod - def _simple_new(cls, values: TimedeltaArray, name: Label = None): - assert isinstance(values, TimedeltaArray) - - result = object.__new__(cls) - result._data = values - result._name = name - result._cache = {} - # For groupby perf. See note in indexes/base about _index_data - result._index_data = values._data - - result._reset_identity() - return result - # ------------------------------------------------------------------- # Rendering Methods From 0c6e8f7bdb6d95a4a5ecb981ecf258c26e0f502a Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 16 Nov 2020 08:10:28 -0800 Subject: [PATCH 3/4] remove antipattern --- pandas/core/indexes/datetimelike.py | 11 ++++++++--- pandas/core/indexes/datetimes.py | 1 + pandas/core/indexes/period.py | 1 + pandas/core/indexes/timedeltas.py | 1 + 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/pandas/core/indexes/datetimelike.py b/pandas/core/indexes/datetimelike.py index cb76bf43e4184..88383cb7bbb6a 100644 --- a/pandas/core/indexes/datetimelike.py +++ b/pandas/core/indexes/datetimelike.py @@ -2,7 +2,7 @@ Base and utility classes for tseries type pandas objects. """ from datetime import datetime -from typing import TYPE_CHECKING, Any, List, Optional, Tuple, TypeVar, Union, cast +from typing import TYPE_CHECKING, Any, List, Optional, Tuple, Type, TypeVar, Union, cast import numpy as np @@ -88,6 +88,7 @@ class DatetimeIndexOpsMixin(NDArrayBackedExtensionIndex): _can_hold_strings = False _data: Union[DatetimeArray, TimedeltaArray, PeriodArray] + _data_cls: Union[Type[DatetimeArray], Type[TimedeltaArray], Type[PeriodArray]] freq: Optional[BaseOffset] freqstr: Optional[str] _resolution_obj: Resolution @@ -101,8 +102,12 @@ class DatetimeIndexOpsMixin(NDArrayBackedExtensionIndex): _hasnans = hasnans # for index / array -agnostic code @classmethod - def _simple_new(cls, values, name: Label = None): - assert isinstance(values, cls.__annotations__["_data"]), type(values) + def _simple_new( + cls, + values: Union[DatetimeArray, TimedeltaArray, PeriodArray], + name: Label = None, + ): + assert isinstance(values, cls._data_cls), type(values) result = object.__new__(cls) result._data = values diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index d84ae4d0803d6..cb1b8836d973f 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -220,6 +220,7 @@ class DatetimeIndex(DatetimeTimedeltaMixin): _typ = "datetimeindex" + _data_cls = DatetimeArray _engine_type = libindex.DatetimeEngine _supports_partial_string_indexing = True diff --git a/pandas/core/indexes/period.py b/pandas/core/indexes/period.py index 16ed01f4a75d1..c0a3b95499b3d 100644 --- a/pandas/core/indexes/period.py +++ b/pandas/core/indexes/period.py @@ -146,6 +146,7 @@ class PeriodIndex(DatetimeIndexOpsMixin, Int64Index): _data: PeriodArray freq: BaseOffset + _data_cls = PeriodArray _engine_type = libindex.PeriodEngine _supports_partial_string_indexing = True diff --git a/pandas/core/indexes/timedeltas.py b/pandas/core/indexes/timedeltas.py index b42d401c027be..b6c626197eaf9 100644 --- a/pandas/core/indexes/timedeltas.py +++ b/pandas/core/indexes/timedeltas.py @@ -103,6 +103,7 @@ class TimedeltaIndex(DatetimeTimedeltaMixin): _typ = "timedeltaindex" + _data_cls = TimedeltaArray _engine_type = libindex.TimedeltaEngine _comparables = ["name", "freq"] From cbd40199147b82f84efa045d4662615bfba1905a Mon Sep 17 00:00:00 2001 From: Brock Date: Mon, 16 Nov 2020 12:04:49 -0800 Subject: [PATCH 4/4] revert annotation in Index._simple_new --- pandas/core/indexes/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 2e5be63c4bd26..cb5641a74e60b 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -418,7 +418,7 @@ def asi8(self): return None @classmethod - def _simple_new(cls, values: np.ndarray, name: Label = None): + def _simple_new(cls, values, name: Label = None): """ We require that we have a dtype compat for the values. If we are passed a non-dtype compat, then coerce using the constructor.