Skip to content

Commit 07257a0

Browse files
pyklerjreback
authored andcommitted
BUG: Fixing == __eq__ operator for MultiIndex ... closes (GH9785)
1 parent bed38f2 commit 07257a0

File tree

4 files changed

+71
-5
lines changed

4 files changed

+71
-5
lines changed

doc/source/whatsnew/v0.16.1.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Bug Fixes
125125
- Bug in ``read_csv`` and ``read_table`` when using ``skip_rows`` parameter if blank lines are present. (:issue:`9832`)
126126

127127
- Bug in ``read_csv()`` interprets ``index_col=True`` as ``1`` (:issue:`9798`)
128-
128+
- Bug in index equality comparisons using ``==`` failing on Index/MultiIndex type incompatibility (:issue:`9875`)
129129
- Bug in which ``SparseDataFrame`` could not take `nan` as a column name (:issue:`8822`)
130130

131131
- Bug in ``Series.quantile`` on empty Series of type ``Datetime`` or ``Timedelta`` (:issue:`9675`)

pandas/core/common.py

+9-2
Original file line numberDiff line numberDiff line change
@@ -2444,7 +2444,10 @@ def _get_dtype_type(arr_or_dtype):
24442444
return np.dtype(arr_or_dtype).type
24452445
elif isinstance(arr_or_dtype, CategoricalDtype):
24462446
return CategoricalDtypeType
2447-
return arr_or_dtype.dtype.type
2447+
try:
2448+
return arr_or_dtype.dtype.type
2449+
except AttributeError:
2450+
raise ValueError('%r is not a dtype' % arr_or_dtype)
24482451

24492452

24502453
def is_any_int_dtype(arr_or_dtype):
@@ -2515,7 +2518,11 @@ def is_floating_dtype(arr_or_dtype):
25152518

25162519

25172520
def is_bool_dtype(arr_or_dtype):
2518-
tipo = _get_dtype_type(arr_or_dtype)
2521+
try:
2522+
tipo = _get_dtype_type(arr_or_dtype)
2523+
except ValueError:
2524+
# this isn't even a dtype
2525+
return False
25192526
return issubclass(tipo, np.bool_)
25202527

25212528
def is_categorical(array):

pandas/core/index.py

+1-2
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, opname)
5554
result = func(np.asarray(other))
5655

5756
# technically we could support bool dtyped Index

pandas/tests/test_index.py

+60
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

@@ -4088,6 +4106,48 @@ def test_index_name_retained(self):
40884106
df_expected = df_expected.set_index('z')
40894107
tm.assert_frame_equal(result, df_expected)
40904108

4109+
def test_equals_operator(self):
4110+
# For issue #9785
4111+
self.assertTrue((self.index == self.index).all())
4112+
4113+
def test_index_compare(self):
4114+
# For issue #9785
4115+
index_unequal = Index(['foo', 'bar', 'baz'])
4116+
index_equal = Index([
4117+
('foo', 'one'), ('foo', 'two'), ('bar', 'one'),
4118+
('baz', 'two'), ('qux', 'one'), ('qux', 'two')
4119+
], tupleize_cols=False)
4120+
# Testing Numpy Results Equivelent
4121+
assert_array_equal(
4122+
index_unequal.equals(self.index),
4123+
index_unequal == self.index,
4124+
err_msg = 'Index compared with MultiIndex failed',
4125+
)
4126+
assert_array_equal(
4127+
self.index.equals(index_unequal),
4128+
self.index == index_unequal,
4129+
err_msg = 'MultiIndex compared with Index failed',
4130+
)
4131+
assert_array_equal(
4132+
self.index.equals(index_equal),
4133+
self.index == index_equal,
4134+
err_msg = 'MultiIndex compared with Similar Index failed',
4135+
)
4136+
assert_array_equal(
4137+
index_equal.equals(self.index),
4138+
index_equal == self.index,
4139+
err_msg = 'Index compared with Similar MultiIndex failed',
4140+
)
4141+
# Testing that the result is true for the index_equal case
4142+
self.assertTrue(
4143+
(self.index == index_equal).all(),
4144+
msg='Assert Index compared with Similar MultiIndex match'
4145+
)
4146+
self.assertTrue(
4147+
(index_equal == self.index).all(),
4148+
msg='Assert MultiIndex compared with Similar Index match'
4149+
)
4150+
40914151

40924152
def test_get_combined_index():
40934153
from pandas.core.index import _get_combined_index

0 commit comments

Comments
 (0)