Skip to content

Commit 1f23ebc

Browse files
committed
BUG: Ensure that Index._data is an ndarray
BUG: Ensure that Index._data is an ndarray Split from #23623, where it was causing issues with infer_dtype.
1 parent 58a59bd commit 1f23ebc

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

pandas/core/indexes/base.py

+4
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,10 @@ def _simple_new(cls, values, name=None, dtype=None, **kwargs):
522522
values = cls(values, name=name, dtype=dtype,
523523
**kwargs)._ndarray_values
524524

525+
if isinstance(values, (ABCSeries, cls)):
526+
values = np.asarray(values._values)
527+
528+
assert isinstance(values, np.ndarray)
525529
result = object.__new__(cls)
526530
result._data = values
527531
result.name = name

pandas/tests/indexes/test_base.py

+6
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,12 @@ def test_constructor_cast(self):
504504
with pytest.raises(ValueError, match=msg):
505505
Index(["a", "b", "c"], dtype=float)
506506

507+
def test_constructor_unwraps_index(self):
508+
a = pd.Index([True, False])
509+
b = pd.Index(a)
510+
expected = np.array([True, False], dtype=object)
511+
tm.assert_numpy_array_equal(b._data, expected)
512+
507513
def test_view_with_args(self):
508514

509515
restricted = ['unicodeIndex', 'strIndex', 'catIndex', 'boolIndex',

pandas/tests/indexes/test_numeric.py

+6
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,12 @@ def test_constructor_coercion_signed_to_unsigned(self, uint_dtype):
628628
with pytest.raises(OverflowError, match=msg):
629629
Index([-1], dtype=uint_dtype)
630630

631+
def test_constructor_unwraps_index(self):
632+
idx = pd.Index([1, 2])
633+
result = pd.Int64Index(idx)
634+
expected = np.array([1, 2], dtype='int64')
635+
tm.assert_numpy_array_equal(result._data, expected)
636+
631637
def test_coerce_list(self):
632638
# coerce things
633639
arr = Index([1, 2, 3, 4])

0 commit comments

Comments
 (0)