Skip to content

Automatically keep categorical type on merge #24093

Open
@Gerenuk

Description

@Gerenuk

Code Sample, a copy-pastable example if possible

import pandas as pd
dd1=pd.DataFrame({"a":pd.Series(["x"], dtype="category")})
dd2=pd.DataFrame({"a":pd.Series(["x", "y"], dtype="category")})
dd3=dd1.merge(dd2)
dd3.dtypes  # categorical turns into object type

Problem description

It would be nice if when merging on categorical columns, their categorical nature would remain by an automatic union_categoricals. Currently only categoricals with identical values remain categorical and merge on non-identical categoricals becomes object type.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugCategoricalCategorical Data TypeDtype ConversionsUnexpected or buggy dtype conversionsReshapingConcat, Merge/Join, Stack/Unstack, Explode

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions