Skip to content

Commit 1ab2770

Browse files
committed
Check whether _simple_new always receives an ndarray
1 parent 18878d7 commit 1ab2770

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
@@ -526,6 +526,7 @@ def _generate(cls, start, end, periods, name, freq,
526526
freq=freq, name=name)
527527
else:
528528
index = _generate_regular_range(start, end, periods, freq)
529+
529530
else:
530531

531532
if tz is not None:
@@ -549,6 +550,7 @@ def _generate(cls, start, end, periods, name, freq,
549550
freq=freq, name=name)
550551
else:
551552
index = _generate_regular_range(start, end, periods, freq)
553+
552554
if tz is not None and getattr(index, 'tz', None) is None:
553555
arr = conversion.tz_localize_to_utc(_ensure_int64(index),
554556
tz,
@@ -609,6 +611,8 @@ def _simple_new(cls, values, name=None, freq=None, tz=None,
609611
dtype=dtype, **kwargs)
610612
values = np.array(values, copy=False)
611613

614+
assert isinstance(values, np.ndarray)
615+
612616
if is_object_dtype(values):
613617
return cls(values, name=name, freq=freq, tz=tz,
614618
dtype=dtype, **kwargs).values
@@ -1060,7 +1064,7 @@ def unique(self, level=None):
10601064
else:
10611065
naive = self
10621066
result = super(DatetimeIndex, naive).unique(level=level)
1063-
return self._simple_new(result, name=self.name, tz=self.tz,
1067+
return self._simple_new(result.values, name=self.name, tz=self.tz,
10641068
freq=self.freq)
10651069

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