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

Fix DataFrame from Series with different CategoricalIndexes #14157

Merged
merged 4 commits into from
Sep 25, 2023
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
7 changes: 7 additions & 0 deletions python/cudf/cudf/core/indexed_frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -5438,6 +5438,13 @@ def _is_same_dtype(lhs_dtype, rhs_dtype):
# for matching column dtype.
if lhs_dtype == rhs_dtype:
return True
elif (
is_categorical_dtype(lhs_dtype)
and is_categorical_dtype(rhs_dtype)
and lhs_dtype.categories.dtype == rhs_dtype.categories.dtype
):
# OK if categories are not all the same
return True
elif (
is_categorical_dtype(lhs_dtype)
and not is_categorical_dtype(rhs_dtype)
Expand Down
13 changes: 13 additions & 0 deletions python/cudf/cudf/tests/test_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -10408,6 +10408,19 @@ def test_dataframe_init_from_nested_dict():
assert_eq(pdf, gdf)


def test_init_from_2_categoricalindex_series_diff_categories():
s1 = cudf.Series(
[39, 6, 4], index=cudf.CategoricalIndex(["female", "male", "unknown"])
)
s2 = cudf.Series(
[2, 152, 2, 242, 150],
index=cudf.CategoricalIndex(["f", "female", "m", "male", "unknown"]),
)
result = cudf.DataFrame([s1, s2])
expected = pd.DataFrame([s1.to_pandas(), s2.to_pandas()])
assert_eq(result, expected, check_dtype=False)


def test_data_frame_values_no_cols_but_index():
result = cudf.DataFrame(index=range(5)).values
expected = pd.DataFrame(index=range(5)).values
Expand Down