Skip to content

Commit 7ad0188

Browse files
committed
Check whether _simple_new always receives an ndarray
1 parent 13de1d2 commit 7ad0188

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
@@ -527,6 +527,7 @@ def _generate(cls, start, end, periods, name, freq,
527527
freq=freq, name=name)
528528
else:
529529
index = _generate_regular_range(start, end, periods, freq)
530+
530531
else:
531532

532533
if tz is not None:
@@ -550,6 +551,7 @@ def _generate(cls, start, end, periods, name, freq,
550551
freq=freq, name=name)
551552
else:
552553
index = _generate_regular_range(start, end, periods, freq)
554+
553555
if tz is not None and getattr(index, 'tz', None) is None:
554556
arr = conversion.tz_localize_to_utc(_ensure_int64(index),
555557
tz,
@@ -610,6 +612,8 @@ def _simple_new(cls, values, name=None, freq=None, tz=None,
610612
dtype=dtype, **kwargs)
611613
values = np.array(values, copy=False)
612614

615+
assert isinstance(values, np.ndarray)
616+
613617
if is_object_dtype(values):
614618
return cls(values, name=name, freq=freq, tz=tz,
615619
dtype=dtype, **kwargs).values
@@ -1058,7 +1062,7 @@ def unique(self, level=None):
10581062
else:
10591063
naive = self
10601064
result = super(DatetimeIndex, naive).unique(level=level)
1061-
return self._simple_new(result, name=self.name, tz=self.tz,
1065+
return self._simple_new(result.values, name=self.name, tz=self.tz,
10621066
freq=self.freq)
10631067

10641068
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)