Skip to content

BUG: Fixed merge on dtype equal categories #19553

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 8, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -679,6 +679,9 @@ Categorical
- Bug in :func:`pandas.api.types.union_categoricals` returning the wrong result
when all the categoricals had the same categories, but in a different order.
This affected :func:`pandas.concat` with Categorical data (:issue:`19096`).
- Bug in :func:`pandas.merge` when joining on a ``Categorical`` column where
the categories were unordered and had the same values but in a different
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks fine

maybe should elevate to a subsection to highlite this

order (:issue:`19551`)
- Bug in ``Categorical.equals`` between two unordered categories with the same categories, but in a different order (:issue:`16603`)
- Bug in :meth:`Index.astype` with a categorical dtype where the resultant index is not converted to a :class:`CategoricalIndex` for all types of index (:issue:`18630`)
- Bug in :meth:`Series.astype` and ``Categorical.astype()`` where an existing categorical data does not get updated (:issue:`10696`, :issue:`18593`)
Expand Down
10 changes: 9 additions & 1 deletion pandas/core/reshape/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

from pandas import (Categorical, DataFrame,
Index, MultiIndex, Timedelta)
from pandas.core.arrays.categorical import _recode_for_categories
from pandas.core.frame import _merge_doc
from pandas.core.dtypes.common import (
is_datetime64tz_dtype,
Expand Down Expand Up @@ -1540,8 +1541,15 @@ def _factorize_keys(lk, rk, sort=True):
is_categorical_dtype(rk) and
lk.is_dtype_equal(rk)):
klass = libhashtable.Int64Factorizer

if lk.categories.equals(rk.categories):
rk = rk.codes
else:
# Same categories in different orders -> recode
rk = _recode_for_categories(rk.codes, rk.categories, lk.categories)

lk = _ensure_int64(lk.codes)
rk = _ensure_int64(rk.codes)
rk = _ensure_int64(rk)
elif is_int_or_datetime_dtype(lk) and is_int_or_datetime_dtype(rk):
klass = libhashtable.Int64Factorizer
lk = _ensure_int64(com._values_from_object(lk))
Expand Down
19 changes: 19 additions & 0 deletions pandas/tests/reshape/merge/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -1643,6 +1643,25 @@ def test_merge_categorical(self):
result = pd.merge(cleft, cright, how='left', left_on='b', right_on='c')
tm.assert_frame_equal(result, expected)

def tests_merge_categorical_unordered_equal(self):
# GH-19551
df1 = DataFrame({
'Foo': Categorical(['A', 'B', 'C'], categories=['A', 'B', 'C']),
'Left': ['A0', 'B0', 'C0'],
})

df2 = DataFrame({
'Foo': Categorical(['C', 'B', 'A'], categories=['C', 'B', 'A']),
'Right': ['C1', 'B1', 'A1'],
})
result = pd.merge(df1, df2, on=['Foo'])
expected = DataFrame({
'Foo': pd.Categorical(['A', 'B', 'C']),
'Left': ['A0', 'B0', 'C0'],
'Right': ['A1', 'B1', 'C1'],
})
assert_frame_equal(result, expected)

def test_other_columns(self, left, right):
# non-merge columns should preserve if possible
right = right.assign(Z=right.Z.astype('category'))
Expand Down