Skip to content

ENH: Cast ndarray-like datetime64 arrays to Index properly #7468

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 16, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ def __new__(cls, data, dtype=None, copy=False, name=None, fastpath=False,
if copy:
subarr = subarr.copy()

elif hasattr(data, '__array__'):
return Index(np.asarray(data), dtype=dtype, copy=copy, name=name,
**kwargs)
elif np.isscalar(data):
cls._scalar_data_error(data)
else:
Expand Down
15 changes: 10 additions & 5 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,12 +177,17 @@ def test_constructor_ndarray_like(self):
# it should be possible to convert any object that satisfies the numpy
# ndarray interface directly into an Index
class ArrayLike(object):
def __init__(self, array):
self.array = array
def __array__(self, dtype=None):
return np.arange(5)
return self.array

expected = pd.Index(np.arange(5))
result = pd.Index(ArrayLike())
self.assertTrue(result.equals(expected))
for array in [np.arange(5),
np.array(['a', 'b', 'c']),
pd.date_range('2000-01-01', periods=3).values]:
expected = pd.Index(array)
result = pd.Index(ArrayLike(array))
self.assertTrue(result.equals(expected))

def test_index_ctor_infer_periodindex(self):
from pandas import period_range, PeriodIndex
Expand Down Expand Up @@ -447,7 +452,7 @@ def test_intersection(self):
assertRaisesRegexp(TypeError, "iterable", first.intersection, 0.5)

idx1 = Index([1, 2, 3, 4, 5], name='idx')
# if target has the same name, it is preserved
# if target has the same name, it is preserved
idx2 = Index([3, 4, 5, 6, 7], name='idx')
expected2 = Index([3, 4, 5], name='idx')
result2 = idx1.intersection(idx2)
Expand Down