Skip to content

ENH: check for __array__ instead of ndarray subclasses when creating an Index #7270

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
May 29, 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: 2 additions & 1 deletion pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2038,7 +2038,8 @@ def intersection(*seqs):
def _asarray_tuplesafe(values, dtype=None):
from pandas.core.index import Index

if not isinstance(values, (list, tuple, np.ndarray)):
if not (isinstance(values, (list, tuple))
or hasattr(values, '__array__')):
values = list(values)
elif isinstance(values, Index):
return values.values
Expand Down
12 changes: 12 additions & 0 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,18 @@ def test_constructor_from_series(self):
result = pd.infer_freq(df['date'])
self.assertEqual(result,'MS')

def test_constructor_ndarray_like(self):
# GH 5460#issuecomment-44474502
# it should be possible to convert any object that satisfies the numpy
# ndarray interface directly into an Index
class ArrayLike(object):
def __array__(self, dtype=None):
return np.arange(5)

expected = pd.Index(np.arange(5))
result = pd.Index(ArrayLike())
self.assertTrue(result.equals(expected))

def test_index_ctor_infer_periodindex(self):
from pandas import period_range, PeriodIndex
xp = period_range('2012-1-1', freq='M', periods=3)
Expand Down