Skip to content

Bad Error message / docs for Categorical.fillna with dict value #19705

Open
@TomAugspurger

Description

In [1]: import pandas as pd

In [2]: pd.Categorical(['a', 'b', None]).fillna({2: 'a'})
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-2-099b4b7d5652> in <module>()
----> 1 pd.Categorical(['a', 'b', None]).fillna({2: 'a'})

~/Envs/pandas-dev/lib/python3.6/site-packages/pandas/pandas/util/_decorators.py in wrapper(*args, **kwargs)
    136                 else:
    137                     kwargs[new_arg_name] = new_arg_value
--> 138             return func(*args, **kwargs)
    139         return wrapper
    140     return _deprecate_kwarg

~/Envs/pandas-dev/lib/python3.6/site-packages/pandas/pandas/core/arrays/categorical.py in fillna(self, value, method, limit)
   1661                 raise TypeError('"value" parameter must be a scalar, dict '
   1662                                 'or Series, but you passed a '
-> 1663                                 '"{0}"'.format(type(value).__name__))
   1664
   1665         return self._constructor(values, categories=self.categories,

TypeError: "value" parameter must be a scalar, dict or Series, but you passed a "dict"

I suppose that mappings don't make sense for Categorical.fillna

Also... this seems strange...

In [10]: pd.Categorical(['a', 'b', None]).fillna(pd.Series(['a'], index=[0, 1, 2]))
Out[10]:
[a, a, a]
Categories (2, object): [a, b]

We shouldn't be affecting valid values here.

Perhaps best to just raise for all series / dict like? Unless I'm missing something.

Metadata

Assignees

No one assigned

    Labels

    CategoricalCategorical Data TypeDocsError ReportingIncorrect or improved errors from pandasMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolate

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions