Skip to content

BUG: Fixed union_categoricals with unordered cats #19097

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 3 commits into from
Jan 7, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 4 additions & 1 deletion doc/source/whatsnew/v0.23.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,11 @@ Numeric
Categorical
^^^^^^^^^^^

- Bug in ``Categorical.equals`` between two unordered categories with the same categories, but in a different order (:issue:`16603`)
-
- 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 ``Categorical.equals`` between two unordered categories with the same categories, but in a different order (:issue:`16603`)
-

Other
Expand Down
11 changes: 10 additions & 1 deletion pandas/core/dtypes/concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,16 @@ def _maybe_unwrap(x):
# identical categories - fastpath
categories = first.categories
ordered = first.ordered
new_codes = np.concatenate([c.codes for c in to_union])

if all(first.categories.equals(other.categories)
for other in to_union[1:]):
new_codes = np.concatenate([c.codes for c in to_union])
else:
codes = [first.codes] + [_recode_for_categories(other.codes,
other.categories,
first.categories)
for other in to_union[1:]]
new_codes = np.concatenate(codes)

if sort_categories and not ignore_order and ordered:
raise TypeError("Cannot use sort_categories=True with "
Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/reshape/test_concat.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,15 @@ def test_concat_categorical(self):
tm.assert_series_equal(pd.concat([s1, s2], ignore_index=True), exp)
tm.assert_series_equal(s1.append(s2, ignore_index=True), exp)

def test_union_categorical_same_categories_different_order(self):
# https://github.com/pandas-dev/pandas/issues/19096
a = pd.Series(Categorical(['a', 'b', 'c'], categories=['a', 'b', 'c']))
b = pd.Series(Categorical(['a', 'b', 'c'], categories=['b', 'a', 'c']))
result = pd.concat([a, b], ignore_index=True)
expected = pd.Series(Categorical(['a', 'b', 'c', 'a', 'b', 'c'],
categories=['a', 'b', 'c']))
tm.assert_series_equal(result, expected)

def test_concat_categorical_coercion(self):
# GH 13524

Expand Down
9 changes: 9 additions & 0 deletions pandas/tests/reshape/test_union_categoricals.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,15 @@ def test_union_categorical_same_category(self):
categories=['x', 'y', 'z'])
tm.assert_categorical_equal(res, exp)

def test_union_categorical_same_categories_different_order(self):
# https://github.com/pandas-dev/pandas/issues/19096
c1 = Categorical(['a', 'b', 'c'], categories=['a', 'b', 'c'])
c2 = Categorical(['a', 'b', 'c'], categories=['b', 'a', 'c'])
result = union_categoricals([c1, c2])
expected = Categorical(['a', 'b', 'c', 'a', 'b', 'c'],
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe add the concat example too?

categories=['a', 'b', 'c'])
tm.assert_categorical_equal(result, expected)

def test_union_categoricals_ordered(self):
c1 = Categorical([1, 2, 3], ordered=True)
c2 = Categorical([1, 2, 3], ordered=False)
Expand Down