Skip to content

Categorical fillna with custom objects raises TypeError #21097

Closed
@jbrockmendel

Description

@jbrockmendel

This behavior appears new in 0.23.0.

Setup copied from #21002:

from pandas.core.base import StringMixin

class County(StringMixin):
    name = u'San Sebastián'
    state = u'PR'
    def __unicode__(self):
        return self.name + u', ' + self.state

cat = pd.Categorical([County() for n in range(61)])
>>> cat.fillna(cat[0])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python2.7/site-packages/pandas/util/_decorators.py", line 177, in wrapper
    return func(*args, **kwargs)
  File "/usr/local/lib/python2.7/site-packages/pandas/core/arrays/categorical.py", line 1769, in fillna
    '"{0}"'.format(type(value).__name__))
TypeError: "value" parameter must be a scalar, dict or Series, but you passed a "County"

I don't see any reason why passing one of the categories to fillna should be disallowed, so it looks like the issue is with being too strict about what qualifies as a scalar.

Metadata

Metadata

Assignees

No one assigned

    Labels

    CategoricalCategorical Data TypeMissing-datanp.nan, pd.NaT, pd.NA, dropna, isnull, interpolateRegressionFunctionality that used to work in a prior pandas versiongood first issue

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions