From 8a45a2ef0743a2f9adc8a90dc1424c41660eff6f Mon Sep 17 00:00:00 2001 From: P-Tillmann Date: Mon, 28 Jun 2021 14:58:52 +0200 Subject: [PATCH 1/2] test upcasting to object when concatinating on categorical indexes with non-identical categories --- .../tests/reshape/concat/test_categorical.py | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/pandas/tests/reshape/concat/test_categorical.py b/pandas/tests/reshape/concat/test_categorical.py index d8b5f19c6a745..54dc1244f4f6c 100644 --- a/pandas/tests/reshape/concat/test_categorical.py +++ b/pandas/tests/reshape/concat/test_categorical.py @@ -202,3 +202,24 @@ def test_categorical_concat_gh7864(self): dfa = df1.append(df2) tm.assert_index_equal(df["grade"].cat.categories, dfa["grade"].cat.categories) + + def test_categorical_index_upcast(self): + # GH 17629 + # test upcasting to object when concatinating on categorical indexes + # with non-identical categories + + a = pd.DataFrame({"foo": [1, 2]}, index=Categorical(["foo", "bar"])) + b = pd.DataFrame({"foo": [4, 3]}, index=Categorical(["baz", "bar"])) + + res = pd.concat([a, b]) + exp = pd.DataFrame({"foo": [1, 2, 4, 3]}, index=["foo", "bar", "baz", "bar"]) + + tm.assert_equal(res, exp) + + a = pd.Series([1, 2], index=Categorical(["foo", "bar"])) + b = pd.Series([4, 3], index=Categorical(["baz", "bar"])) + + res = pd.concat([a, b]) + exp = pd.Series([1, 2, 4, 3], index=["foo", "bar", "baz", "bar"]) + + tm.assert_equal(res, exp) From ed47241e370e82b9463dce3d0a2af38a43397f42 Mon Sep 17 00:00:00 2001 From: P-Tillmann Date: Mon, 28 Jun 2021 15:29:03 +0200 Subject: [PATCH 2/2] fixed flake8 issue --- pandas/tests/reshape/concat/test_categorical.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pandas/tests/reshape/concat/test_categorical.py b/pandas/tests/reshape/concat/test_categorical.py index 54dc1244f4f6c..aba14fd2fcd77 100644 --- a/pandas/tests/reshape/concat/test_categorical.py +++ b/pandas/tests/reshape/concat/test_categorical.py @@ -208,18 +208,18 @@ def test_categorical_index_upcast(self): # test upcasting to object when concatinating on categorical indexes # with non-identical categories - a = pd.DataFrame({"foo": [1, 2]}, index=Categorical(["foo", "bar"])) - b = pd.DataFrame({"foo": [4, 3]}, index=Categorical(["baz", "bar"])) + a = DataFrame({"foo": [1, 2]}, index=Categorical(["foo", "bar"])) + b = DataFrame({"foo": [4, 3]}, index=Categorical(["baz", "bar"])) res = pd.concat([a, b]) - exp = pd.DataFrame({"foo": [1, 2, 4, 3]}, index=["foo", "bar", "baz", "bar"]) + exp = DataFrame({"foo": [1, 2, 4, 3]}, index=["foo", "bar", "baz", "bar"]) tm.assert_equal(res, exp) - a = pd.Series([1, 2], index=Categorical(["foo", "bar"])) - b = pd.Series([4, 3], index=Categorical(["baz", "bar"])) + a = Series([1, 2], index=Categorical(["foo", "bar"])) + b = Series([4, 3], index=Categorical(["baz", "bar"])) res = pd.concat([a, b]) - exp = pd.Series([1, 2, 4, 3], index=["foo", "bar", "baz", "bar"]) + exp = Series([1, 2, 4, 3], index=["foo", "bar", "baz", "bar"]) tm.assert_equal(res, exp)