Skip to content

Commit dc2ca92

Browse files
reidy-pvictor
authored and
victor
committed
BUG: DatetimeIndex._data should return an ndarray (pandas-dev#20912)
1 parent 34b02c9 commit dc2ca92

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

pandas/core/indexes/base.py

+4
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,10 @@ 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+
510+
# _simple_new expects an ndarray
511+
values = getattr(values, 'values', values)
512+
509513
return self._simple_new(values, **attributes)
510514

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

pandas/core/indexes/datetimes.py

+16-12
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
from pandas.core.dtypes.common import (
1313
_INT64_DTYPE,
1414
_NS_DTYPE,
15-
is_object_dtype,
1615
is_datetime64_dtype,
1716
is_datetimetz,
1817
is_dtype_equal,
@@ -551,10 +550,11 @@ def _generate(cls, start, end, periods, name, freq,
551550
index = _generate_regular_range(start, end, periods, freq)
552551

553552
if tz is not None and getattr(index, 'tz', None) is None:
554-
index = conversion.tz_localize_to_utc(_ensure_int64(index),
555-
tz,
556-
ambiguous=ambiguous)
557-
index = index.view(_NS_DTYPE)
553+
arr = conversion.tz_localize_to_utc(_ensure_int64(index),
554+
tz,
555+
ambiguous=ambiguous)
556+
557+
index = DatetimeIndex(arr)
558558

559559
# index is localized datetime64 array -> have to convert
560560
# start/end as well to compare
@@ -575,7 +575,9 @@ def _generate(cls, start, end, periods, name, freq,
575575
index = index[1:]
576576
if not right_closed and len(index) and index[-1] == end:
577577
index = index[:-1]
578-
index = cls._simple_new(index, name=name, freq=freq, tz=tz)
578+
579+
index = cls._simple_new(index.values, name=name, freq=freq, tz=tz)
580+
579581
return index
580582

581583
def _convert_for_op(self, value):
@@ -606,12 +608,14 @@ def _simple_new(cls, values, name=None, freq=None, tz=None,
606608
dtype=dtype, **kwargs)
607609
values = np.array(values, copy=False)
608610

609-
if is_object_dtype(values):
610-
return cls(values, name=name, freq=freq, tz=tz,
611-
dtype=dtype, **kwargs).values
612-
elif not is_datetime64_dtype(values):
611+
if not is_datetime64_dtype(values):
613612
values = _ensure_int64(values).view(_NS_DTYPE)
614613

614+
values = getattr(values, 'values', values)
615+
616+
assert isinstance(values, np.ndarray), "values is not an np.ndarray"
617+
assert is_datetime64_dtype(values)
618+
615619
result = super(DatetimeIndex, cls)._simple_new(values, freq, tz,
616620
**kwargs)
617621
result.name = name
@@ -1000,7 +1004,7 @@ def unique(self, level=None):
10001004
else:
10011005
naive = self
10021006
result = super(DatetimeIndex, naive).unique(level=level)
1003-
return self._simple_new(result, name=self.name, tz=self.tz,
1007+
return self._simple_new(result.values, name=self.name, tz=self.tz,
10041008
freq=self.freq)
10051009

10061010
def union(self, other):
@@ -1855,7 +1859,7 @@ def _generate_regular_range(start, end, periods, freq):
18551859
"if a 'period' is given.")
18561860

18571861
data = np.arange(b, e, stride, dtype=np.int64)
1858-
data = DatetimeIndex._simple_new(data, None, tz=tz)
1862+
data = DatetimeIndex._simple_new(data.view(_NS_DTYPE), None, tz=tz)
18591863
else:
18601864
if isinstance(start, Timestamp):
18611865
start = start.to_pydatetime()

pandas/io/pytables.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -2472,7 +2472,8 @@ def _get_index_factory(self, klass):
24722472
if klass == DatetimeIndex:
24732473
def f(values, freq=None, tz=None):
24742474
# data are already in UTC, localize and convert if tz present
2475-
result = DatetimeIndex._simple_new(values, None, freq=freq)
2475+
result = DatetimeIndex._simple_new(values.values, None,
2476+
freq=freq)
24762477
if tz is not None:
24772478
result = result.tz_localize('UTC').tz_convert(tz)
24782479
return result

pandas/tests/indexes/test_base.py

+1-1
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)