Skip to content

Commit 38ca1e0

Browse files
committed
Merge pull request #7468 from shoyer/ndarray-like-to-index
ENH: Cast ndarray-like datetime64 arrays to Index properly
2 parents 7684b6e + 785f407 commit 38ca1e0

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
@@ -150,6 +150,9 @@ def __new__(cls, data, dtype=None, copy=False, name=None, fastpath=False,
150150
if copy:
151151
subarr = subarr.copy()
152152

153+
elif hasattr(data, '__array__'):
154+
return Index(np.asarray(data), dtype=dtype, copy=copy, name=name,
155+
**kwargs)
153156
elif np.isscalar(data):
154157
cls._scalar_data_error(data)
155158
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)