Skip to content
Advertisement

Check if dataframe column is Categorical

I can’t seem to get a simple dtype check working with Pandas’ improved Categoricals in v0.15+. Basically I just want something like is_categorical(column) -> True/False.

JavaScript

We can see that the dtype for the categorical column is ‘category’:

JavaScript

And normally we can do a dtype check by just comparing to the name of the dtype:

JavaScript

But this doesn’t seem to work when trying to check if the x column is categorical:

JavaScript

Is there any way to do these types of checks in pandas v0.15+?

Advertisement

Answer

Use the name property to do the comparison instead, it should always work because it’s just a string:

JavaScript

So, to sum up, you can end up with a simple, straightforward function:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement