Skip to content

Commit 309067a

Browse files
TafkasTomAugspurger
authored andcommitted
return empty MultiIndex for symmetrical difference on equal MultiIndexes (pandas-dev#16486)
(cherry picked from commit d31ffdb)
1 parent 31e20ae commit 309067a

File tree

4 files changed

+18
-2
lines changed

4 files changed

+18
-2
lines changed

doc/source/whatsnew/v0.20.2.txt

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Bug Fixes
4141
- Silenced a warning on some Windows environments about "tput: terminal attributes: No such device or address" when
4242
detecting the terminal size. This fix only applies to python 3 (:issue:`16496`)
4343
- Bug in using ``pathlib.Path`` or ``py.path.local`` objects with io functions (:issue:`16291`)
44+
- Bug in ``Index.symmetric_difference()`` on two equal MultiIndex's, results in a TypeError (:issue `13490`)
4445
- Bug in ``DataFrame.update()`` with ``overwrite=False`` and ``NaN values`` (:issue:`15593`)
4546
- Passing an invalid engine to :func:`read_csv` now raises an informative
4647
``ValueError`` rather than ``UnboundLocalError``. (:issue:`16511`)

pandas/core/indexes/multi.py

+6
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,12 @@ def view(self, cls=None):
414414
return result
415415

416416
def _shallow_copy_with_infer(self, values=None, **kwargs):
417+
# On equal MultiIndexes the difference is empty.
418+
# Therefore, an empty MultiIndex is returned GH13490
419+
if len(values) == 0:
420+
return MultiIndex(levels=[[] for _ in range(self.nlevels)],
421+
labels=[[] for _ in range(self.nlevels)],
422+
**kwargs)
417423
return self._shallow_copy(values, **kwargs)
418424

419425
@Appender(_index_shared_docs['_shallow_copy'])

pandas/tests/indexes/test_base.py

-2
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,6 @@ def test_constructor_ndarray_like(self):
188188
# it should be possible to convert any object that satisfies the numpy
189189
# ndarray interface directly into an Index
190190
class ArrayLike(object):
191-
192191
def __init__(self, array):
193192
self.array = array
194193

@@ -246,7 +245,6 @@ def test_index_ctor_infer_nan_nat(self):
246245
[np.timedelta64('nat'), np.nan],
247246
[pd.NaT, np.timedelta64('nat')],
248247
[np.timedelta64('nat'), pd.NaT]]:
249-
250248
tm.assert_index_equal(Index(data), exp)
251249
tm.assert_index_equal(Index(np.array(data, dtype=object)), exp)
252250

pandas/tests/indexing/test_multiindex.py

+11
Original file line numberDiff line numberDiff line change
@@ -697,6 +697,17 @@ def test_multiindex_slice_first_level(self):
697697
index=range(30, 71))
698698
tm.assert_frame_equal(result, expected)
699699

700+
def test_multiindex_symmetric_difference(self):
701+
# GH 13490
702+
idx = MultiIndex.from_product([['a', 'b'], ['A', 'B']],
703+
names=['a', 'b'])
704+
result = idx ^ idx
705+
assert result.names == idx.names
706+
707+
idx2 = idx.copy().rename(['A', 'B'])
708+
result = idx ^ idx2
709+
assert result.names == [None, None]
710+
700711

701712
class TestMultiIndexSlicers(object):
702713

0 commit comments

Comments
 (0)