Skip to content

BUG: _get_dtype accepts CategoricalDtype but throws TypeError with a Categorical #16817

Closed
@topper-123

Description

@topper-123

Problem description

I find it inconistent that _get_dtype accept a CategoricalDtype, but will thow TypeError on Categoricals.

Code Sample, a copy-pastable example if possible

>>> cats = pd.Categorical([1,2,3])
>>> pd.core.dtypes.common._get_dtype(cats.dtype)
category
>>> pd.core.dtypes.common._get_dtype(cats)
TypeError: data type not understood

Expected Output

>>> cats = pd.Categorical([1,2,3])
>>> pd.core.dtypes.common._get_dtype(cats.dtype)
category
>>> pd.core.dtypes.common._get_dtype(cats)
category

Proposed solution

A solution could be to check for extension type (maybe just categorical dtype, not sure) + that arr_or_dtype has a dtype attribute:

def _get_dtypes(arr_or_dtype):
   ... 
    elif isinstance(arr_or_dtype, IntervalDtype):
        return arr_or_dtype   # old code
   elif is_extension_type(arr_or_dtype) and hasattr(arr_or_dtype, 'dtype'):   # new code
        return arr_or_dtype.dtype   # new code
    elif isinstance(arr_or_dtype, string_types):   # old code
      ...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions