Skip to content

pd.isna(Categorical) casts to object array #18856

@TomAugspurger

Description

@TomAugspurger

In pd.isna, we convert instances of pd.Categorical a numpy array:

elif isinstance(obj, list) or hasattr(obj, '__array__'):
return _isna_ndarraylike(np.asarray(obj))

We should be able to avoid that by calling obj.values.isna(), which just operates on the codes. To summarize:

  • pd.isna(Categorical(...)) coerces
  • DataFrame.isna / coerces
  • Series.isna / pd.isna(series) is fine, we handle that above
  • Categorical(...).isna is fine

So the cases to handle:

>>> s = pd.Categorical(['a', 'b']) >>> pd.isna(s) >>> pd.isna(pd.DataFrame({'A': s})) >>> pd.DataFrame({"A": s}).isna()

Metadata

Metadata

Assignees

No one assigned

    Labels

    CategoricalCategorical Data TypePerformanceMemory or execution speed performance

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions