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
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
8 changes: 7 additions & 1 deletion pandas/core/indexes/base.py
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@
ABCSeries, ABCDataFrame,
ABCMultiIndex,
ABCPeriodIndex, ABCTimedeltaIndex, ABCDatetimeIndex,
ABCDateOffset)
ABCDateOffset, ABCIndexClass)
from pandas.core.dtypes.missing import isna, array_equivalent
from pandas.core.dtypes.cast import maybe_cast_to_integer_array
from pandas.core.dtypes.common import (
@@ -522,6 +522,12 @@ def _simple_new(cls, values, name=None, dtype=None, **kwargs):
values = cls(values, name=name, dtype=dtype,
**kwargs)._ndarray_values

if isinstance(values, (ABCSeries, ABCIndexClass)):
# Index._data must always be an ndarray.
# This is no-copy for when _values is an ndarray,
# which should be always at this point.
values = np.asarray(values._values)

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

def test_constructor_unwraps_index(self, indices):
if isinstance(indices, pd.MultiIndex):
raise pytest.skip("MultiIndex has no ._data")
a = indices
b = type(a)(a)
tm.assert_equal(a._data, b._data)

def test_view_with_args(self):

restricted = ['unicodeIndex', 'strIndex', 'catIndex', 'boolIndex',
28 changes: 14 additions & 14 deletions pandas/tests/series/test_operators.py
Original file line number Diff line number Diff line change
@@ -189,20 +189,7 @@ def test_scalar_na_logical_ops_corners(self):
operator.and_,
operator.or_,
operator.xor,
pytest.param(ops.rand_,
marks=pytest.mark.xfail(reason="GH#22092 Index "
"implementation returns "
"Index",
raises=AssertionError,
strict=True)),
pytest.param(ops.ror_,
marks=pytest.mark.xfail(reason="GH#22092 Index "
"implementation raises",
raises=ValueError, strict=True)),
pytest.param(ops.rxor,
marks=pytest.mark.xfail(reason="GH#22092 Index "
"implementation raises",
raises=TypeError, strict=True))

])
def test_logical_ops_with_index(self, op):
# GH#22092, GH#19792
@@ -221,6 +208,19 @@ def test_logical_ops_with_index(self, op):
result = op(ser, idx2)
assert_series_equal(result, expected)

@pytest.mark.parametrize("op, expected", [
(ops.rand_, pd.Index([False, True])),
(ops.ror_, pd.Index([False, True])),
(ops.rxor, pd.Index([])),
])
def test_reverse_ops_with_index(self, op, expected):
# https://github.com/pandas-dev/pandas/pull/23628
# multi-set Index ops are buggy, so let's avoid duplicates...
ser = Series([True, False])
idx = Index([False, True])
result = op(ser, idx)
tm.assert_index_equal(result, expected)

def test_logical_ops_label_based(self):
# GH#4947
# logical ops should be label based