Skip to content

Commit

Permalink
BUG: Load data from a CategoricalIndex for dtype comparison, closes p…
Browse files Browse the repository at this point in the history
  • Loading branch information
thequackdaddy committed Jun 21, 2017
1 parent 8a98f5e commit 95348c1
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.21.0.txt
Expand Up @@ -100,6 +100,7 @@ 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
Expand Down
8 changes: 6 additions & 2 deletions pandas/core/reshape/merge.py
Expand Up @@ -11,7 +11,7 @@
import pandas.compat as compat

from pandas import (Categorical, Series, DataFrame,
Index, MultiIndex, Timedelta)
Index, MultiIndex, Timedelta, CategoricalIndex)
from pandas.core.frame import _merge_doc
from pandas.core.dtypes.common import (
is_datetime64tz_dtype,
Expand Down Expand Up @@ -1441,9 +1441,13 @@ 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
lk.is_dtype_equal(rk)):
ldata.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):
Expand Down
21 changes: 21 additions & 0 deletions pandas/tests/test_join.py
Expand Up @@ -192,3 +192,24 @@ def test_inner_join_indexer2():

exp_ridx = np.array([0, 1, 2, 3], dtype=np.int64)
assert_almost_equal(ridx, exp_ridx)


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']),
'Int1': [0, 1, 0, 1, 0, 0]}
a = pd.DataFrame(a)

b = {'Cat': pd.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']

c = pd.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)

0 comments on commit 95348c1

Please sign in to comment.