diff --git a/doc/source/whatsnew/v0.20.3.txt b/doc/source/whatsnew/v0.20.3.txt index c730142450ea64..cbd86ae4c27224 100644 --- a/doc/source/whatsnew/v0.20.3.txt +++ b/doc/source/whatsnew/v0.20.3.txt @@ -51,6 +51,7 @@ Indexing ^^^^^^^^ - Bug in ``Float64Index`` causing an empty array instead of ``None`` to be returned from ``.get(np.nan)`` on a Series whose index did not contain any ``NaN`` s (:issue:`8569`) +- Fixed a bug that prevented joining on a categorical MultiIndex (:issue:`16627`). I/O ^^^ diff --git a/doc/source/whatsnew/v0.21.0.txt b/doc/source/whatsnew/v0.21.0.txt index c4429eef143274..046a6c885bd24b 100644 --- a/doc/source/whatsnew/v0.21.0.txt +++ b/doc/source/whatsnew/v0.21.0.txt @@ -100,7 +100,6 @@ Indexing - When called with a null slice (e.g. ``df.iloc[:]``), the ``.iloc`` and ``.loc`` indexers return a shallow copy of the original object. Previously they returned the original object. (:issue:`13873`). - When called on an unsorted ``MultiIndex``, the ``loc`` indexer now will raise ``UnsortedIndexError`` only if proper slicing is used on non-sorted levels (:issue:`16734`). -- Fixed a bug that prevented joining on a categorical MultiIndex (:issue:`13873`). I/O diff --git a/pandas/core/indexes/category.py b/pandas/core/indexes/category.py index d9e0c218bfafc2..d13636e8b43e23 100644 --- a/pandas/core/indexes/category.py +++ b/pandas/core/indexes/category.py @@ -560,6 +560,9 @@ def take(self, indices, axis=0, allow_fill=True, na_value=-1) return self._create_from_codes(taken) + def is_dtype_equal(self, other): + return self._data.is_dtype_equal(other) + take_nd = take def map(self, mapper): diff --git a/pandas/core/reshape/merge.py b/pandas/core/reshape/merge.py index 99d9af28ac0191..c8083af8a9fdf2 100644 --- a/pandas/core/reshape/merge.py +++ b/pandas/core/reshape/merge.py @@ -1441,13 +1441,9 @@ def _factorize_keys(lk, rk, sort=True): rk = rk.values # if we exactly match in categories, allow us to use codes - if isinstance(lk, CategoricalIndex): - ldata = lk._data - else: - ldata = lk if (is_categorical_dtype(lk) and is_categorical_dtype(rk) and - ldata.is_dtype_equal(rk)): + lk.is_dtype_equal(rk)): return lk.codes, rk.codes, len(lk.categories) if is_int_or_datetime_dtype(lk) and is_int_or_datetime_dtype(rk): diff --git a/pandas/tests/test_join.py b/pandas/tests/test_join.py index 5d29c5355f8806..215ad21940f4a2 100644 --- a/pandas/tests/test_join.py +++ b/pandas/tests/test_join.py @@ -1,11 +1,11 @@ # -*- coding: utf-8 -*- import numpy as np -from pandas import Index +from pandas import Index, DataFrame, Categorical, merge from pandas._libs import join as _join import pandas.util.testing as tm -from pandas.util.testing import assert_almost_equal +from pandas.util.testing import assert_almost_equal, assert_frame_equal class TestIndexer(object): @@ -196,20 +196,38 @@ def test_inner_join_indexer2(): def test_merge_join_categorical_multiindex(): # From issue 16627 - import pandas as pd - a = {'Cat1': pd.Categorical(['a', 'b', 'a', 'c', 'a', 'b'], - ['a', 'b', 'c']), + a = {'Cat1': Categorical(['a', 'b', 'a', 'c', 'a', 'b'], + ['a', 'b', 'c']), 'Int1': [0, 1, 0, 1, 0, 0]} - a = pd.DataFrame(a) + a = DataFrame(a) - b = {'Cat': pd.Categorical(['a', 'b', 'c', 'a', 'b', 'c'], - ['a', 'b', 'c']), + b = {'Cat': Categorical(['a', 'b', 'c', 'a', 'b', 'c'], + ['a', 'b', 'c']), 'Int': [0, 0, 0, 1, 1, 1], 'Factor': [1.1, 1.2, 1.3, 1.4, 1.5, 1.6]} - b = pd.DataFrame(b).set_index(['Cat', 'Int'])['Factor'] + b = DataFrame(b).set_index(['Cat', 'Int'])['Factor'] - c = pd.merge(a, b.reset_index(), left_on=['Cat1', 'Int1'], - right_on=['Cat', 'Int'], how='left') + c = merge(a, b.reset_index(), left_on=['Cat1', 'Int1'], + right_on=['Cat', 'Int'], how='left') d = a.join(b, on=['Cat1', 'Int1']) c = c.drop(['Cat', 'Int'], axis=1) - assert_almost_equal(c, d) + assert_frame_equal(c, d) + + a = {'Cat1': Categorical(['a', 'b', 'a', 'c', 'a', 'b'], + ['b', 'a', 'c'], + ordered=True), + 'Int1': [0, 1, 0, 1, 0, 0]} + a = DataFrame(a) + + b = {'Cat': Categorical(['a', 'b', 'c', 'a', 'b', 'c'], + ['b', 'a', 'c'], + ordered=True), + 'Int': [0, 0, 0, 1, 1, 1], + 'Factor': [1.1, 1.2, 1.3, 1.4, 1.5, 1.6]} + b = DataFrame(b).set_index(['Cat', 'Int'])['Factor'] + + c = merge(a, b.reset_index(), left_on=['Cat1', 'Int1'], + right_on=['Cat', 'Int'], how='left') + d = a.join(b, on=['Cat1', 'Int1']) + c = c.drop(['Cat', 'Int'], axis=1) + assert_frame_equal(c, d)