Check if dataframe column is Categorical

后端 未结 5 1929
灰色年华
灰色年华 2020-12-29 00:53

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

5条回答
  •  一整个雨季
    2020-12-29 01:40

    I ran into this thread looking for the exact same functionality, and also found out another option, right from the pandas documentation here.

    It looks like the canonical way to check if a pandas dataframe column is a categorical Series should be the following:

    hasattr(column_to_check, 'cat')
    

    So, as per the example given in the initial question, this would be:

    hasattr(df.x, 'cat') #True
    

提交回复
热议问题