Skip to content

Commit

Permalink
Fix DataFrame from Series with different CategoricalIndexes (#14157)
Browse files Browse the repository at this point in the history
closes #14130

Authors:
  - Matthew Roeschke (https://github.com/mroeschke)

Approvers:
  - GALI PREM SAGAR (https://github.com/galipremsagar)

URL: #14157
  • Loading branch information
mroeschke authored Sep 25, 2023
1 parent 3f47b5d commit 036c07d
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
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

0 comments on commit 036c07d

Please sign in to comment.