From 1f23ebc92f6c15bf1c2dd7d7a1ddbfc5debd81a2 Mon Sep 17 00:00:00 2001 From: Tom Augspurger Date: Sun, 11 Nov 2018 15:01:13 -0600 Subject: [PATCH] BUG: Ensure that Index._data is an ndarray BUG: Ensure that Index._data is an ndarray Split from https://github.com/pandas-dev/pandas/pull/23623, where it was causing issues with infer_dtype. --- pandas/core/indexes/base.py | 4 ++++ pandas/tests/indexes/test_base.py | 6 ++++++ pandas/tests/indexes/test_numeric.py | 6 ++++++ 3 files changed, 16 insertions(+) diff --git a/pandas/core/indexes/base.py b/pandas/core/indexes/base.py index 263de57d32f31..e0a64897b3905 100644 --- a/pandas/core/indexes/base.py +++ b/pandas/core/indexes/base.py @@ -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 diff --git a/pandas/tests/indexes/test_base.py b/pandas/tests/indexes/test_base.py index 4a3efe22926f7..054efa00cd892 100644 --- a/pandas/tests/indexes/test_base.py +++ b/pandas/tests/indexes/test_base.py @@ -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', diff --git a/pandas/tests/indexes/test_numeric.py b/pandas/tests/indexes/test_numeric.py index c125db16bcbff..d1ad2308d19e5 100644 --- a/pandas/tests/indexes/test_numeric.py +++ b/pandas/tests/indexes/test_numeric.py @@ -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])