Skip to content

Commit cc6ab25

Browse files
committed
Check whether _simple_new always receives an ndarray
1 parent 917917e commit cc6ab25

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

pandas/core/indexes/base.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,9 @@ def _shallow_copy(self, values=None, **kwargs):
506506
attributes.update(kwargs)
507507
if not len(values) and 'dtype' not in kwargs:
508508
attributes['dtype'] = self.dtype
509+
from pandas import DatetimeIndex
510+
if isinstance(values, DatetimeIndex):
511+
values = values.values
509512
return self._simple_new(values, **attributes)
510513

511514
def _shallow_copy_with_infer(self, values=None, **kwargs):

pandas/core/indexes/datetimes.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,7 @@ def _generate(cls, start, end, periods, name, freq,
525525
freq=freq, name=name)
526526
else:
527527
index = _generate_regular_range(start, end, periods, freq)
528+
528529
else:
529530

530531
if tz is not None:
@@ -548,6 +549,7 @@ def _generate(cls, start, end, periods, name, freq,
548549
freq=freq, name=name)
549550
else:
550551
index = _generate_regular_range(start, end, periods, freq)
552+
551553
if tz is not None and getattr(index, 'tz', None) is None:
552554
arr = conversion.tz_localize_to_utc(_ensure_int64(index),
553555
tz,
@@ -608,6 +610,8 @@ def _simple_new(cls, values, name=None, freq=None, tz=None,
608610
dtype=dtype, **kwargs)
609611
values = np.array(values, copy=False)
610612

613+
assert isinstance(values, np.ndarray)
614+
611615
if is_object_dtype(values):
612616
return cls(values, name=name, freq=freq, tz=tz,
613617
dtype=dtype, **kwargs).values
@@ -1002,7 +1006,7 @@ def unique(self, level=None):
10021006
else:
10031007
naive = self
10041008
result = super(DatetimeIndex, naive).unique(level=level)
1005-
return self._simple_new(result, name=self.name, tz=self.tz,
1009+
return self._simple_new(result.values, name=self.name, tz=self.tz,
10061010
freq=self.freq)
10071011

10081012
def union(self, other):

pandas/tests/indexes/test_base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ def test_index_ctor_infer_periodindex(self):
329329
])
330330
def test_constructor_simple_new(self, vals, dtype):
331331
index = Index(vals, name=dtype)
332-
result = index._simple_new(index, dtype)
332+
result = index._simple_new(index.values, dtype)
333333
tm.assert_index_equal(result, index)
334334

335335
@pytest.mark.parametrize("vals", [

0 commit comments

Comments
 (0)