Skip to content

Ensure Index._data is an ndarray #23628

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
Show file tree
Hide file tree
Changes from 4 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/indexes/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,9 @@ 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)):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should use ABCIndexClass here? it’s acrually more clear

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can never remember whether ABCIndexClass or ABCIndex includes subclasses, so I was happy when I realized I could just use cls here :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ABCIndexClass - it’s just more consistent in the code

values = np.asarray(values._values)

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):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can make this more general by using the indices fixture here.

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)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rather than specificying testing a single index, see my above comment.

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
10 changes: 7 additions & 3 deletions pandas/tests/series/test_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
from pandas import (
Categorical, DataFrame, Index, NaT, Series, bdate_range, date_range, isna)
from pandas.core import ops
from pandas.core.indexes.base import InvalidIndexError
import pandas.core.nanops as nanops
import pandas.util.testing as tm
from pandas.util.testing import (
Expand Down Expand Up @@ -198,11 +199,14 @@ def test_scalar_na_logical_ops_corners(self):
pytest.param(ops.ror_,
marks=pytest.mark.xfail(reason="GH#22092 Index "
"implementation raises",
raises=ValueError, strict=True)),
raises=InvalidIndexError,
strict=True)),
pytest.param(ops.rxor,
marks=pytest.mark.xfail(reason="GH#22092 Index "
"implementation raises",
raises=TypeError, strict=True))
"implementation returns "
"Index",
raises=AssertionError,
strict=True))
])
def test_logical_ops_with_index(self, op):
# GH#22092, GH#19792
Expand Down