Skip to content

Commit 785f407

Browse files
committed
ENH: Cast ndarray-like datetime64 arrays to Index properly
It turns out that the ndarray-like arrays of dtype datetime64 were not being properly cast to an Index, because -- due to a bug with np.datetime64 -- calling np.asarray(x, dtype=object) if x is an ndarray of type datetime64 results in an *integer* array. This PR adds tests and a work around to pd.Index.__new__. Related #5460
1 parent 8d209de commit 785f407

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

pandas/core/index.py

+3
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,9 @@ def __new__(cls, data, dtype=None, copy=False, name=None, fastpath=False,
148148
if copy:
149149
subarr = subarr.copy()
150150

151+
elif hasattr(data, '__array__'):
152+
return Index(np.asarray(data), dtype=dtype, copy=copy, name=name,
153+
**kwargs)
151154
elif np.isscalar(data):
152155
cls._scalar_data_error(data)
153156
else:

pandas/tests/test_index.py

+10-5
Original file line numberDiff line numberDiff line change
@@ -177,12 +177,17 @@ def test_constructor_ndarray_like(self):
177177
# it should be possible to convert any object that satisfies the numpy
178178
# ndarray interface directly into an Index
179179
class ArrayLike(object):
180+
def __init__(self, array):
181+
self.array = array
180182
def __array__(self, dtype=None):
181-
return np.arange(5)
183+
return self.array
182184

183-
expected = pd.Index(np.arange(5))
184-
result = pd.Index(ArrayLike())
185-
self.assertTrue(result.equals(expected))
185+
for array in [np.arange(5),
186+
np.array(['a', 'b', 'c']),
187+
pd.date_range('2000-01-01', periods=3).values]:
188+
expected = pd.Index(array)
189+
result = pd.Index(ArrayLike(array))
190+
self.assertTrue(result.equals(expected))
186191

187192
def test_index_ctor_infer_periodindex(self):
188193
from pandas import period_range, PeriodIndex
@@ -447,7 +452,7 @@ def test_intersection(self):
447452
assertRaisesRegexp(TypeError, "iterable", first.intersection, 0.5)
448453

449454
idx1 = Index([1, 2, 3, 4, 5], name='idx')
450-
# if target has the same name, it is preserved
455+
# if target has the same name, it is preserved
451456
idx2 = Index([3, 4, 5, 6, 7], name='idx')
452457
expected2 = Index([3, 4, 5], name='idx')
453458
result2 = idx1.intersection(idx2)

0 commit comments

Comments
 (0)