Skip to content

Commit

Permalink
BUG: Ensure that Index._data is an ndarray
Browse files Browse the repository at this point in the history
BUG: Ensure that Index._data is an ndarray

Split from pandas-dev#23623, where it was
causing issues with infer_dtype.
  • Loading branch information
TomAugspurger committed Nov 11, 2018
1 parent 58a59bd commit 1f23ebc
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,10 @@ def _simple_new(cls, values, name=None, dtype=None, **kwargs):
values = cls(values, name=name, dtype=dtype,
**kwargs)._ndarray_values

if isinstance(values, (ABCSeries, cls)):
values = np.asarray(values._values)

assert isinstance(values, np.ndarray)
result = object.__new__(cls)
result._data = values
result.name = name
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/indexes/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,12 @@ def test_constructor_cast(self):
with pytest.raises(ValueError, match=msg):
Index(["a", "b", "c"], dtype=float)

def test_constructor_unwraps_index(self):
a = pd.Index([True, False])
b = pd.Index(a)
expected = np.array([True, False], dtype=object)
tm.assert_numpy_array_equal(b._data, expected)

def test_view_with_args(self):

restricted = ['unicodeIndex', 'strIndex', 'catIndex', 'boolIndex',
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/indexes/test_numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,12 @@ def test_constructor_coercion_signed_to_unsigned(self, uint_dtype):
with pytest.raises(OverflowError, match=msg):
Index([-1], dtype=uint_dtype)

def test_constructor_unwraps_index(self):
idx = pd.Index([1, 2])
result = pd.Int64Index(idx)
expected = np.array([1, 2], dtype='int64')
tm.assert_numpy_array_equal(result._data, expected)

def test_coerce_list(self):
# coerce things
arr = Index([1, 2, 3, 4])
Expand Down

0 comments on commit 1f23ebc

Please sign in to comment.