Skip to content

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

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

Closed
TomAugspurger opened this issue Aug 29, 2014 · 1 comment · Fixed by #8146
Closed

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

TomAugspurger opened this issue Aug 29, 2014 · 1 comment · Fixed by #8146
Labels
Bug Categorical Categorical Data Type
Milestone

Comments

@TomAugspurger
Copy link
Contributor

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)

@jreback
Copy link
Contributor

jreback commented Aug 29, 2014

yep, i will fix (easy).

FYI, you should use com.is_category_dtype when testing in any event (in internal code)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Categorical Categorical Data Type
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants