Skip to content

BUG: Categorical.from_codes casts nullable int categories to object #39649

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

Closed
arw2019 opened this issue Feb 7, 2021 · 2 comments · Fixed by #45167
Closed

BUG: Categorical.from_codes casts nullable int categories to object #39649

arw2019 opened this issue Feb 7, 2021 · 2 comments · Fixed by #45167
Labels
Bug Categorical Categorical Data Type Dtype Conversions Unexpected or buggy dtype conversions NA - MaskedArrays Related to pd.NA and nullable extension arrays
Milestone

Comments

@arw2019
Copy link
Member

arw2019 commented Feb 7, 2021

For int64 the construction yields:

In [18]: import numpy as np
    ...: import pandas as pd
    ...: import pandas._testing as tm
    ...: 
    ...: integer_dtype="int64"
    ...: 
    ...: cats = pd.array(range(5), dtype=integer_dtype)
    ...: codes = np.random.randint(5, size=3)
    ...: categorical_dtype = pd.CategoricalDtype(cats)
    ...: arr = pd.Categorical.from_codes(codes, dtype=categorical_dtype)
    ...: arr
Out[18]: 
[0, 2, 0]
Categories (5, int64): [0, 1, 2, 3, 4]

whereas for nullable int categories:

In [19]: import numpy as np
    ...: import pandas as pd
    ...: import pandas._testing as tm
    ...: 
    ...: integer_dtype="Int64"
    ...: 
    ...: cats = pd.array(range(5), dtype=integer_dtype)
    ...: codes = np.random.randint(5, size=3)
    ...: categorical_dtype = pd.CategoricalDtype(cats)
    ...: arr = pd.Categorical.from_codes(codes, dtype=categorical_dtype)
    ...: arr
Out[19]: 
[3, 3, 4]
Categories (5, object): [0, 1, 2, 3, 4]

I expect instead in the nullable int case:

Out[19]: 
[3, 3, 4]
Categories (5, Int64): [0, 1, 2, 3, 4]
@arw2019 arw2019 added Bug Needs Triage Issue that has not been reviewed by a pandas team member Categorical Categorical Data Type NA - MaskedArrays Related to pd.NA and nullable extension arrays Dtype Conversions Unexpected or buggy dtype conversions labels Feb 7, 2021
@anders-wind
Copy link

anders-wind commented Feb 8, 2021

I can confirm that this happens on version 1.2.1.

@jorisvandenbossche
Copy link
Member

In general we still need to add support for "nullable" categorical (however it may look like).

Very practical reason currently is that we don't yet support storing EAs in the index, and Categorical uses Index as the categories. Now, personally I would say that for the nullable dtypes, we could also look into storing the categories as a plain array instead of Index.

@jorisvandenbossche jorisvandenbossche removed Bug Needs Triage Issue that has not been reviewed by a pandas team member labels Feb 10, 2021
@mroeschke mroeschke added the Bug label Aug 15, 2021
@jreback jreback added this to the 1.4 milestone Jan 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type Dtype Conversions Unexpected or buggy dtype conversions NA - MaskedArrays Related to pd.NA and nullable extension arrays
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants