Skip to content

Commit 55cfabb

Browse files
jbrockmendeljreback
authored andcommitted
REF: be stricter about what we pass to _simple_new (#31055)
1 parent 024925a commit 55cfabb

File tree

8 files changed

+12
-20
lines changed

8 files changed

+12
-20
lines changed

pandas/core/arrays/period.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,9 @@ def __init__(self, values, freq=None, dtype=None, copy=False):
169169
self._dtype = PeriodDtype(freq)
170170

171171
@classmethod
172-
def _simple_new(cls, values, freq=None, **kwargs):
172+
def _simple_new(cls, values: np.ndarray, freq=None, **kwargs):
173173
# alias for PeriodArray.__init__
174+
assert isinstance(values, np.ndarray) and values.dtype == "i8"
174175
return cls(values, freq=freq, **kwargs)
175176

176177
@classmethod

pandas/core/indexes/base.py

+2-10
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@
5050
from pandas.core.dtypes.generic import (
5151
ABCCategorical,
5252
ABCDataFrame,
53-
ABCDatetimeArray,
5453
ABCDatetimeIndex,
5554
ABCIndexClass,
5655
ABCIntervalIndex,
@@ -460,11 +459,7 @@ def _simple_new(cls, values, name=None, dtype=None):
460459
461460
Must be careful not to recurse.
462461
"""
463-
if isinstance(values, (ABCSeries, ABCIndexClass)):
464-
# Index._data must always be an ndarray.
465-
# This is no-copy for when _values is an ndarray,
466-
# which should be always at this point.
467-
values = np.asarray(values._values)
462+
assert isinstance(values, np.ndarray), type(values)
468463

469464
result = object.__new__(cls)
470465
result._data = values
@@ -510,17 +505,14 @@ def _get_attributes_dict(self):
510505
def _shallow_copy(self, values=None, **kwargs):
511506
if values is None:
512507
values = self.values
508+
513509
attributes = self._get_attributes_dict()
514510
attributes.update(kwargs)
515511
if not len(values) and "dtype" not in kwargs:
516512
attributes["dtype"] = self.dtype
517513

518514
# _simple_new expects an the type of self._data
519515
values = getattr(values, "_values", values)
520-
if isinstance(values, ABCDatetimeArray):
521-
# `self.values` returns `self` for tz-aware, so we need to unwrap
522-
# more specifically
523-
values = values.asi8
524516

525517
return self._simple_new(values, **attributes)
526518

pandas/core/indexes/datetimes.py

-4
Original file line numberDiff line numberDiff line change
@@ -281,10 +281,6 @@ def _simple_new(cls, values, name=None, freq=None, tz=None, dtype=None):
281281
freq = values.freq
282282
values = values._data
283283

284-
# DatetimeArray._simple_new will accept either i8 or M8[ns] dtypes
285-
if isinstance(values, DatetimeIndex):
286-
values = values._data
287-
288284
dtype = tz_to_dtype(tz)
289285
dtarr = DatetimeArray._simple_new(values, freq=freq, dtype=dtype)
290286
assert isinstance(dtarr, DatetimeArray)

pandas/core/indexes/interval.py

+2
Original file line numberDiff line numberDiff line change
@@ -259,6 +259,8 @@ def _simple_new(cls, array, name, closed=None):
259259
closed : Any
260260
Ignored.
261261
"""
262+
assert isinstance(array, IntervalArray), type(array)
263+
262264
result = IntervalMixin.__new__(cls)
263265
result._data = array
264266
result.name = name

pandas/core/indexes/numeric.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ class NumericIndex(Index):
5757

5858
def __new__(cls, data=None, dtype=None, copy=False, name=None):
5959
cls._validate_dtype(dtype)
60+
name = maybe_extract_name(name, data, cls)
6061

6162
# Coerce to ndarray if not already ndarray or Index
6263
if not isinstance(data, (np.ndarray, Index)):
@@ -82,7 +83,7 @@ def __new__(cls, data=None, dtype=None, copy=False, name=None):
8283
# GH#13601, GH#20285, GH#27125
8384
raise ValueError("Index data must be 1-dimensional")
8485

85-
name = maybe_extract_name(name, data, cls)
86+
subarr = np.asarray(subarr)
8687
return cls._simple_new(subarr, name=name)
8788

8889
@classmethod

pandas/core/indexes/period.py

-2
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,6 @@ def _simple_new(cls, values, name=None, freq=None, **kwargs):
250250
freq = Period._maybe_convert_freq(freq)
251251
values = PeriodArray(values, freq=freq)
252252

253-
if not isinstance(values, PeriodArray):
254-
raise TypeError("PeriodIndex._simple_new only accepts PeriodArray")
255253
result = object.__new__(cls)
256254
result._data = values
257255
# For groupby perf. See note in indexes/base about _index_data

pandas/core/indexes/range.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ def __new__(
114114
return cls._simple_new(rng, dtype=dtype, name=name)
115115

116116
@classmethod
117-
def from_range(cls, data, name=None, dtype=None):
117+
def from_range(cls, data: range, name=None, dtype=None) -> "RangeIndex":
118118
"""
119119
Create RangeIndex from a range object.
120120

pandas/core/indexes/timedeltas.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ def __new__(
183183
def _simple_new(cls, values, name=None, freq=None, dtype=_TD_DTYPE):
184184
# `dtype` is passed by _shallow_copy in corner cases, should always
185185
# be timedelta64[ns] if present
186+
186187
if not isinstance(values, TimedeltaArray):
187188
values = TimedeltaArray._simple_new(values, dtype=dtype, freq=freq)
188189
else:
@@ -409,7 +410,8 @@ def insert(self, loc, item):
409410
new_i8s = np.concatenate(
410411
(self[:loc].asi8, [item.view(np.int64)], self[loc:].asi8)
411412
)
412-
return self._shallow_copy(new_i8s, freq=freq)
413+
tda = type(self._data)._simple_new(new_i8s, freq=freq)
414+
return self._shallow_copy(tda)
413415
except (AttributeError, TypeError):
414416

415417
# fall back to object index

0 commit comments

Comments
 (0)