Skip to content

REF: share _simple_new #37872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 17, 2020
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because this is the base class it should be ArrayLike? (or we can leave this fix for #36092)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, its specifically np.ndarray

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, I guess you want to make Index np.ndarray specific and have ExtensionIndex handle EA backed indexes but in that case some other type annotations in base would need to be changed e,g, _data: Union[ExtensionArray, np.ndarray] so that methods in the base class that currently handle EA can be simplified and overridden.

In #37898, im getting error: Unsupported left operand type for - ("ExtensionArray") in the base class since def _values(self) -> Union[ExtensionArray, np.ndarray] so atm the base class is not EA agnostic.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated to get revert this annotation

"""
We require that we have a dtype compat for the values. If we are passed
a non-dtype compat, then coerce using the constructor.
Expand Down
1 change: 0 additions & 1 deletion pandas/core/indexes/category.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

# --------------------------------------------------------------------
Expand Down
15 changes: 15 additions & 0 deletions pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 1 addition & 15 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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 = {}
result._no_setting_name = False
# For groupby perf. See note in indexes/base about _index_data
result._index_data = values._data
result._reset_identity()
return result

# --------------------------------------------------------------------

@cache_readonly
Expand Down
1 change: 0 additions & 1 deletion pandas/core/indexes/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
22 changes: 0 additions & 22 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
16 changes: 1 addition & 15 deletions pandas/core/indexes/timedeltas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down