Skip to content
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

Add test for concat with categorical + datetime dataframes #45527

Closed
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
30 changes: 30 additions & 0 deletions pandas/tests/reshape/concat/test_categorical.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from datetime import datetime

import numpy as np
import pytest

from pandas.core.dtypes.dtypes import CategoricalDtype

Expand Down Expand Up @@ -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):
Copy link
Member

Choose a reason for hiding this comment

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

Please write the cases down explicitly. We do not gain anything through the parametrization and this is really hard to read

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I can make those changes. Before setting off the CI to fail again though, as mentioned in this comment, my PyTests are passing locally, but not on the CI. Could do with some help on why this might be?

Locally I get below to pass:

    def test_categorical_datetime_concat(self):
        # GH 39443
        # 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)

But in the CI (based on previous failure) - this will fail with:

E       AssertionError: Attributes of DataFrame.iloc[:, 0] (column name="x") are different
E       
E       Attribute "dtype" are different
E       [left]:  CategoricalDtype(categories=['2021-01-01', '2021-01-02'], ordered=False)
E       [right]: datetime64[ns]

Looks like it links to #14016

# 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
Expand Down