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

API: provide a to_enum method for Categorical dtypes? #16749

Closed
chris-b1 opened this issue Jun 21, 2017 · 2 comments
Closed

API: provide a to_enum method for Categorical dtypes? #16749

chris-b1 opened this issue Jun 21, 2017 · 2 comments
Labels
Categorical Categorical Data Type Enhancement Needs Discussion Requires discussion from core team before further action

Comments

@chris-b1
Copy link
Contributor

xref #16015

This might be something nice to provide. Specific usecase I was thinking of is interacting with numba - it understands enums in nopython mode, so it would be a way to work with Categoricals there.

Partially working implementation below, doing something like namedtuple with exec

def categorical_to_enum(categorical, name):
    cats = categorical.categories

    template = """from enum import IntEnum
class {name}(IntEnum):
    NA = -1
{fields}
"""
    assert all(isinstance(x, str) and x.isidentifier()
               for x in cats)

    fields = '\n'.join('    {cat}={code}'.format(cat=cat, code=i)
                       for i, cat in enumerate(cats))
    ns = {}
    exec(template.format(name=name, fields=fields), ns)
    return ns[name]

# example usage
In [81]: c = pd.Categorical(['a', 'b', 'a', 'c'])

In [82]: MyEnum = categorical_to_enum(c, 'MyEnum')

In [83]: MyEnum.c
Out[83]: <MyEnum.c: 2>
@chris-b1 chris-b1 added API Design Categorical Categorical Data Type Needs Discussion Requires discussion from core team before further action labels Jun 21, 2017
@jreback
Copy link
Contributor

jreback commented Jun 22, 2017

I think you can just do this: https://stackoverflow.com/questions/33690064/dynamically-create-an-enum-with-custom-values-in-python

nice idea. maybe .to_enum()

@jreback jreback added this to the Next Major Release milestone Jun 22, 2017
@mroeschke mroeschke removed this from the Contributions Welcome milestone Oct 13, 2022
@mroeschke
Copy link
Member

Thanks for the suggestion but it appears there hasn't been much engagement in this feature in years so closing

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Categorical Categorical Data Type Enhancement Needs Discussion Requires discussion from core team before further action
Projects
None yet
Development

No branches or pull requests

4 participants