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 all 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
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
22 changes: 21 additions & 1 deletion pandas/core/indexes/datetimelike.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -100,6 +101,25 @@ class DatetimeIndexOpsMixin(NDArrayBackedExtensionIndex):
)
_hasnans = hasnans # for index / array -agnostic code

@classmethod
def _simple_new(
cls,
values: Union[DatetimeArray, TimedeltaArray, PeriodArray],
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.

can now just use Hashable, see #37134

Copy link
Member Author

Choose a reason for hiding this comment

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

OK, will edit on subsequent pass

):
assert isinstance(values, cls._data_cls), 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
17 changes: 2 additions & 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 @@ -220,6 +220,7 @@ class DatetimeIndex(DatetimeTimedeltaMixin):

_typ = "datetimeindex"

_data_cls = DatetimeArray
_engine_type = libindex.DatetimeEngine
_supports_partial_string_indexing = True

Expand Down Expand Up @@ -319,20 +320,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
23 changes: 1 addition & 22 deletions pandas/core/indexes/period.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ class PeriodIndex(DatetimeIndexOpsMixin, Int64Index):
_data: PeriodArray
freq: BaseOffset

_data_cls = PeriodArray
_engine_type = libindex.PeriodEngine
_supports_partial_string_indexing = True

Expand Down Expand Up @@ -244,28 +245,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
17 changes: 2 additions & 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 @@ -103,6 +103,7 @@ class TimedeltaIndex(DatetimeTimedeltaMixin):

_typ = "timedeltaindex"

_data_cls = TimedeltaArray
_engine_type = libindex.TimedeltaEngine

_comparables = ["name", "freq"]
Expand Down Expand Up @@ -156,20 +157,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