From 7f20b022dcbd83333ba5722ad47506877ea1b908 Mon Sep 17 00:00:00 2001 From: Ryan Gilmour Date: Fri, 21 Jan 2022 13:19:08 +0000 Subject: [PATCH 1/2] Add test for concat with categorical + datetime dataframes --- .../tests/reshape/concat/test_categorical.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/pandas/tests/reshape/concat/test_categorical.py b/pandas/tests/reshape/concat/test_categorical.py index 5bafd2e8e8503..6286fd6c3ecc8 100644 --- a/pandas/tests/reshape/concat/test_categorical.py +++ b/pandas/tests/reshape/concat/test_categorical.py @@ -1,4 +1,7 @@ +from datetime import datetime + import numpy as np +import pytest from pandas.core.dtypes.dtypes import CategoricalDtype @@ -203,6 +206,33 @@ def test_categorical_concat_gh7864(self): dfa = df1._append(df2) tm.assert_index_equal(df["grade"].cat.categories, dfa["grade"].cat.categories) + @pytest.mark.parametrize( + ("input_1", "input_2", "expected"), + ( # both Catergorical dtypes with datetimes + ( + DataFrame( + {"x": Series(datetime(2021, 1, 1), index=[0], dtype="category")} + ), + DataFrame( + {"x": Series(datetime(2021, 1, 2), index=[1], dtype="category")} + ), + DataFrame({"x": Series([datetime(2021, 1, 1), datetime(2021, 1, 2)])}), + ), + # both Catergorical dtypes, one datetime, one string + ( + DataFrame( + {"x": Series(datetime(2021, 1, 1), index=[0], dtype="category")} + ), + DataFrame({"x": Series("test string", index=[1], dtype="category")}), + DataFrame({"x": Series([datetime(2021, 1, 1), "test string"])}), + ), + ), + ) + def test_categorical_datetime_concat(self, input_1, input_2, expected): + # GH 39443 + result = pd.concat([input_1, input_2]) + tm.assert_equal(result, expected) + def test_categorical_index_upcast(self): # GH 17629 # test upcasting to object when concatinating on categorical indexes From e5807e4809eee3a635547c756df86fa65fc512f5 Mon Sep 17 00:00:00 2001 From: Ryan Gilmour Date: Fri, 21 Jan 2022 14:33:13 +0000 Subject: [PATCH 2/2] Remove pytest parametisation --- .../tests/reshape/concat/test_categorical.py | 48 +++++++++---------- 1 file changed, 23 insertions(+), 25 deletions(-) diff --git a/pandas/tests/reshape/concat/test_categorical.py b/pandas/tests/reshape/concat/test_categorical.py index 6286fd6c3ecc8..c2030828d1cac 100644 --- a/pandas/tests/reshape/concat/test_categorical.py +++ b/pandas/tests/reshape/concat/test_categorical.py @@ -1,7 +1,6 @@ from datetime import datetime import numpy as np -import pytest from pandas.core.dtypes.dtypes import CategoricalDtype @@ -206,31 +205,30 @@ def test_categorical_concat_gh7864(self): dfa = df1._append(df2) tm.assert_index_equal(df["grade"].cat.categories, dfa["grade"].cat.categories) - @pytest.mark.parametrize( - ("input_1", "input_2", "expected"), - ( # both Catergorical dtypes with datetimes - ( - DataFrame( - {"x": Series(datetime(2021, 1, 1), index=[0], dtype="category")} - ), - DataFrame( - {"x": Series(datetime(2021, 1, 2), index=[1], dtype="category")} - ), - DataFrame({"x": Series([datetime(2021, 1, 1), datetime(2021, 1, 2)])}), - ), - # both Catergorical dtypes, one datetime, one string - ( - DataFrame( - {"x": Series(datetime(2021, 1, 1), index=[0], dtype="category")} - ), - DataFrame({"x": Series("test string", index=[1], dtype="category")}), - DataFrame({"x": Series([datetime(2021, 1, 1), "test string"])}), - ), - ), - ) - def test_categorical_datetime_concat(self, input_1, input_2, expected): + def test_categorical_datetime_concat(self): # GH 39443 - result = pd.concat([input_1, input_2]) + # test catergorical dataframes both containing datetimes + df1 = DataFrame( + {"x": Series(datetime(2021, 1, 1), index=[0], dtype="category")} + ) + df2 = DataFrame( + {"x": Series(datetime(2021, 1, 2), index=[1], dtype="category")} + ) + expected = DataFrame( + {"x": Series([datetime(2021, 1, 1), datetime(2021, 1, 2)])} + ) + result = pd.concat([df1, df2]) + tm.assert_equal(result, expected) + + def test_categorical_datetime_and_string_concat(self): + # GH 39443 + # test catergorical dataframes one with datetime, one with a string + df1 = DataFrame( + {"x": Series(datetime(2021, 1, 1), index=[0], dtype="category")} + ) + df2 = DataFrame({"x": Series("test string", index=[1], dtype="category")}) + expected = DataFrame({"x": Series([datetime(2021, 1, 1), "test string"])}) + result = pd.concat([df1, df2]) tm.assert_equal(result, expected) def test_categorical_index_upcast(self):