Skip to content

BUG/API: DataFrame.dtypes == 'categorical' #8143

Closed
@TomAugspurger

Description

@TomAugspurger

Should this work?

In [70]: cat = pd.Categorical(['a', 'b', 'c'])

In [71]: obj = pd.Series(['a', 'b', 'c'])

In [72]: num = pd.Series([1, 2, 3])

In [73]: df = pd.concat([pd.Series(cat), obj, num], axis=1, keys=['cat', 'obj', 'num'])

In [74]: df
Out[74]: 
  cat obj  num
0   a   a    1
1   b   b    2
2   c   c    3

In [75]: df.dtypes
Out[75]: 
cat    category
obj      object
num       int64
dtype: object

In [76]: df.dtypes == 'category'
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-76-48fb3d0c49cf> in <module>()
----> 1 df.dtypes == 'category'

/Users/tom/Envs/py3/lib/python3.4/site-packages/pandas-0.14.1_291_g7a8a030-py3.4-macosx-10.9-x86_64.egg/pandas/core/ops.py in wrapper(self, other)
    579 
    580             # scalars
--> 581             res = na_op(values, other)
    582             if np.isscalar(res):
    583                 raise TypeError('Could not compare %s type with Series'

/Users/tom/Envs/py3/lib/python3.4/site-packages/pandas-0.14.1_291_g7a8a030-py3.4-macosx-10.9-x86_64.egg/pandas/core/ops.py in na_op(x, y)
    526             msg = "Cannot compare a Categorical for op {op} with type {typ}. If you want to \n" \
    527                   "compare values, use 'series <op> np.asarray(cat)'."
--> 528             raise TypeError(msg.format(op=op,typ=type(y)))
    529         if x.dtype == np.object_:
    530             if isinstance(y, list):

TypeError: Cannot compare a Categorical for op <built-in function eq> with type <class 'str'>. If you want to 
compare values, use 'series <op> np.asarray(cat)'.

For comparison

In [77]: df.dtypes == 'object'
Out[77]: 
cat    False
obj     True
num    False
dtype: bool

In [78]: df.dtypes == 'int64'
Out[78]: 
cat    False
obj    False
num     True
dtype: bool

This doesn't work either: In [87]: df.dtypes == df.cat.dtype (raise the same TypeError)

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