Skip to content

Fixing == __eq__ operator for MultiIndex ... closes #9785 #9872

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
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
11 changes: 9 additions & 2 deletions pandas/core/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -2439,7 +2439,10 @@ def _get_dtype_type(arr_or_dtype):
return np.dtype(arr_or_dtype).type
elif isinstance(arr_or_dtype, CategoricalDtype):
return CategoricalDtypeType
return arr_or_dtype.dtype.type
try:
return arr_or_dtype.dtype.type
except AttributeError:
raise ValueError('%r is not a dtype' % arr_or_dtype)


def is_any_int_dtype(arr_or_dtype):
Expand Down Expand Up @@ -2510,7 +2513,11 @@ def is_floating_dtype(arr_or_dtype):


def is_bool_dtype(arr_or_dtype):
tipo = _get_dtype_type(arr_or_dtype)
try:
tipo = _get_dtype_type(arr_or_dtype)
except ValueError:
# this isn't even a dtype
return False
return issubclass(tipo, np.bool_)

def is_categorical(array):
Expand Down
3 changes: 1 addition & 2 deletions pandas/core/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ def _indexOp(opname):
Wrapper function for index comparison operations, to avoid
code duplication.
"""

def wrapper(self, other):
func = getattr(self._data.view(np.ndarray), opname)
func = getattr(self.values, opname)
result = func(np.asarray(other))

# technically we could support bool dtyped Index
Expand Down
60 changes: 60 additions & 0 deletions pandas/tests/test_index.py
Original file line number Diff line number Diff line change
Expand Up @@ -1320,6 +1320,24 @@ def test_groupby(self):
exp = {1: [0, 1], 2: [2, 3, 4]}
tm.assert_dict_equal(groups, exp)

def test_equals_op(self):
# For issue #9785
index_a = Index(['foo', 'bar', 'baz'])
index_b = Index(['foo', 'bar', 'baz', 'qux'])
# Testing Numpy Results Equivelent
assert_array_equal(
index_a.equals(index_a),
index_a == index_a
)
assert_array_equal(
index_a.equals(index_b),
index_a == index_b,
)
assert_array_equal(
index_b.equals(index_a),
index_b == index_a,
)


class Numeric(Base):

Expand Down Expand Up @@ -4075,6 +4093,48 @@ def test_groupby(self):
exp = dict((key, [key]) for key in self.index)
tm.assert_dict_equal(groups, exp)

def test_equals_operator(self):
# For issue #9785
self.assertTrue((self.index == self.index).all())

def test_index_compare(self):
# For issue #9785
index_unequal = Index(['foo', 'bar', 'baz'])
index_equal = Index([
('foo', 'one'), ('foo', 'two'), ('bar', 'one'),
('baz', 'two'), ('qux', 'one'), ('qux', 'two')
], tupleize_cols=False)
# Testing Numpy Results Equivelent
assert_array_equal(
index_unequal.equals(self.index),
index_unequal == self.index,
err_msg = 'Index compared with MultiIndex failed',
)
assert_array_equal(
self.index.equals(index_unequal),
self.index == index_unequal,
err_msg = 'MultiIndex compared with Index failed',
)
assert_array_equal(
self.index.equals(index_equal),
self.index == index_equal,
err_msg = 'MultiIndex compared with Similar Index failed',
)
assert_array_equal(
index_equal.equals(self.index),
index_equal == self.index,
err_msg = 'Index compared with Similar MultiIndex failed',
)
# Testing that the result is true for the index_equal case
self.assertTrue(
(self.index == index_equal).all(),
msg='Assert Index compared with Similar MultiIndex match'
)
self.assertTrue(
(index_equal == self.index).all(),
msg='Assert MultiIndex compared with Similar Index match'
)


def test_get_combined_index():
from pandas.core.index import _get_combined_index
Expand Down