Skip to content

Commit 009df60

Browse files
committed
Fixing == __eq__ operator for MultiIndex ... closes pandas-dev#9785
1 parent 35b20d8 commit 009df60

File tree

3 files changed

+63
-2
lines changed

3 files changed

+63
-2
lines changed

pandas/core/common.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2439,6 +2439,8 @@ def _get_dtype_type(arr_or_dtype):
24392439
return np.dtype(arr_or_dtype).type
24402440
elif isinstance(arr_or_dtype, CategoricalDtype):
24412441
return CategoricalDtypeType
2442+
elif arr_or_dtype is NotImplemented:
2443+
return type(NotImplemented)
24422444
return arr_or_dtype.dtype.type
24432445

24442446

pandas/core/index.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ def _indexOp(opname):
4949
Wrapper function for index comparison operations, to avoid
5050
code duplication.
5151
"""
52-
5352
def wrapper(self, other):
54-
func = getattr(self._data.view(np.ndarray), opname)
53+
func = getattr(self.values.view(np.ndarray), opname)
5554
result = func(np.asarray(other))
5655

5756
# technically we could support bool dtyped Index

pandas/tests/test_index.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1320,6 +1320,24 @@ def test_groupby(self):
13201320
exp = {1: [0, 1], 2: [2, 3, 4]}
13211321
tm.assert_dict_equal(groups, exp)
13221322

1323+
def test_equals_op(self):
1324+
# For issue #9785
1325+
index_a = Index(['foo', 'bar', 'baz'])
1326+
index_b = Index(['foo', 'bar', 'baz', 'qux'])
1327+
# Testing Numpy Results Equivelent
1328+
assert_array_equal(
1329+
index_a.equals(index_a),
1330+
index_a == index_a
1331+
)
1332+
assert_array_equal(
1333+
index_a.equals(index_b),
1334+
index_a == index_b,
1335+
)
1336+
assert_array_equal(
1337+
index_b.equals(index_a),
1338+
index_b == index_a,
1339+
)
1340+
13231341

13241342
class Numeric(Base):
13251343

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

4096+
def test_equals_operator(self):
4097+
# For issue #9785
4098+
self.assertTrue((self.index == self.index).all())
4099+
4100+
def test_index_compare(self):
4101+
# For issue #9785
4102+
index_unequal = Index(['foo', 'bar', 'baz'])
4103+
index_equal = Index([
4104+
('foo', 'one'), ('foo', 'two'), ('bar', 'one'),
4105+
('baz', 'two'), ('qux', 'one'), ('qux', 'two')
4106+
], tupleize_cols=False)
4107+
# Testing Numpy Results Equivelent
4108+
assert_array_equal(
4109+
index_unequal.equals(self.index),
4110+
index_unequal == self.index,
4111+
err_msg = 'Index compared with MultiIndex failed',
4112+
)
4113+
assert_array_equal(
4114+
self.index.equals(index_unequal),
4115+
self.index == index_unequal,
4116+
err_msg = 'MultiIndex compared with Index failed',
4117+
)
4118+
assert_array_equal(
4119+
self.index.equals(index_equal),
4120+
self.index == index_equal,
4121+
err_msg = 'MultiIndex compared with Similar Index failed',
4122+
)
4123+
assert_array_equal(
4124+
index_equal.equals(self.index),
4125+
index_equal == self.index,
4126+
err_msg = 'Index compared with Similar MultiIndex failed',
4127+
)
4128+
# Testing that the result is true for the index_equal case
4129+
self.assertTrue(
4130+
(self.index == index_equal).all(),
4131+
msg='Assert Index compared with Similar MultiIndex match'
4132+
)
4133+
self.assertTrue(
4134+
(index_equal == self.index).all(),
4135+
msg='Assert MultiIndex compared with Similar Index match'
4136+
)
4137+
40784138

40794139
def test_get_combined_index():
40804140
from pandas.core.index import _get_combined_index

0 commit comments

Comments
 (0)