Skip to content

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

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

Open
TomAugspurger opened this issue Feb 14, 2018 · 2 comments
Open

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

TomAugspurger opened this issue Feb 14, 2018 · 2 comments
Labels
Categorical Categorical Data Type Docs Error Reporting Incorrect or improved errors from pandas Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate

Comments

@TomAugspurger
Copy link
Contributor

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.

@jbrockmendel
Copy link
Member

@TomAugspurger is this closed by #19705?

@TomAugspurger
Copy link
Contributor Author

TomAugspurger commented Oct 16, 2020

I don't think so. The docs still say that Categorical.fillna can take a dict input.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Categorical Categorical Data Type Docs Error Reporting Incorrect or improved errors from pandas Missing-data np.nan, pd.NaT, pd.NA, dropna, isnull, interpolate
Projects
None yet
Development

No branches or pull requests

3 participants