# How to check if a column is categorical in dataframes

**URL:** <https://discourse.julialang.org/t/how-to-check-if-a-column-is-categorical-in-dataframes/43830>\
**Category:** General Usage\
**Tags:** dataframes\
**Created:** [July 28, 2020, 6:55pm UTC](https://discourse.julialang.org/t/how-to-check-if-a-column-is-categorical-in-dataframes/43830 "2020-07-28T18:55:36Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![satyabrata\_pal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/satyabrata_pal/32/9035_2.png) [@satyabrata\_pal](https://discourse.julialang.org/u/satyabrata_pal)\
**Post date:** [July 28, 2020, 6:55pm UTC](https://discourse.julialang.org/t/how-to-check-if-a-column-is-categorical-in-dataframes/43830/1 "2020-07-28T18:55:36Z")

</div>

How to check if a dataframw column is categorical?  
│ Row │ A │ B │  
│ │ Cat… │ String │  
├─────┼──────┼────────┤  
│ 1 │ A │ X │  
│ 2 │ B │ X │  
│ 3 │ C │ X │  
│ 4 │ D │ Y │  
│ 5 │ D │ Y │  
│ 6 │ A │ Y │

lets say in the above dataframe the column A is categorical.

Now if I do eltype(df[!, A]) the its returns me

categoricalValue{string, UInt8}

But the eltype can also return categoricalValue{Int}

What would be the best possible way to put a check to see if a column is categorical or not.

---

<div class="post-metadata">

**Author:** ![hendri54](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hendri54/32/9621_2.png) [@hendri54](https://discourse.julialang.org/u/hendri54)\
**Post date:** [July 28, 2020, 7:22pm UTC](https://discourse.julialang.org/t/how-to-check-if-a-column-is-categorical-in-dataframes/43830/2 "2020-07-28T19:22:58Z")

</div>

I would think that checking whether the column is a `CategoricalArray` would be most direct:

```julia
julia> df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"])
4×2 DataFrame
│ Row │ A │ B │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 1 │ M │
│ 2 │ 2 │ F │
│ 3 │ 3 │ F │
│ 4 │ 4 │ M │

julia> typeof(df.B)
Array{String,1}

julia> categorical!(df, :B)
4×2 DataFrame
│ Row │ A │ B │
│ │ Int64 │ Cat… │
├─────┼───────┼──────┤
│ 1 │ 1 │ M │
│ 2 │ 2 │ F │
│ 3 │ 3 │ F │
│ 4 │ 4 │ M │

julia> typeof(df.B)
CategoricalArray{String,1,UInt32,String,CategoricalValue{String,UInt32},Union{}}

julia> isa(df.B, CategoricalArray)
true

```

---

<div class="post-metadata">

**Author:** ![pdeffebach](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/pdeffebach/32/10320_2.png) [@pdeffebach](https://discourse.julialang.org/u/pdeffebach)\
**Post date:** [July 28, 2020, 7:54pm UTC](https://discourse.julialang.org/t/how-to-check-if-a-column-is-categorical-in-dataframes/43830/3 "2020-07-28T19:54:34Z")

</div>

`describe(df)` will also show this information

---

<div class="post-metadata">

**Author:** ![satyabrata\_pal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/satyabrata_pal/32/9035_2.png) [@satyabrata\_pal](https://discourse.julialang.org/u/satyabrata_pal)\
**Post date:** [July 29, 2020, 1:33pm UTC](https://discourse.julialang.org/t/how-to-check-if-a-column-is-categorical-in-dataframes/43830/4 "2020-07-29T13:33:11Z")

</div>

> [@hendri54](#):
>
> `isa(df.B, CategoricalArray)`

This works. All this time I was confused about how to check the “CategoricalValue” type. But checking if it’s a categorical array is indeed the straightforward approach.

---

<div class="post-metadata">

**Author:** ![satyabrata\_pal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/satyabrata_pal/32/9035_2.png) [@satyabrata\_pal](https://discourse.julialang.org/u/satyabrata_pal)\
**Post date:** [July 29, 2020, 1:33pm UTC](https://discourse.julialang.org/t/how-to-check-if-a-column-is-categorical-in-dataframes/43830/5 "2020-07-29T13:33:38Z")

</div>

Thanks @pdeffebach and @hendri54.

---

<div class="post-metadata">

**Author:** ![Soldalma](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/soldalma/32/29388_2.png) [@Soldalma](https://discourse.julialang.org/u/Soldalma)\
**Post date:** [January 22, 2022, 1:24am UTC](https://discourse.julialang.org/t/how-to-check-if-a-column-is-categorical-in-dataframes/43830/6 "2022-01-22T01:24:09Z")

</div>

Is there a way to check if it is ordered?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [January 22, 2022, 11:39am UTC](https://discourse.julialang.org/t/how-to-check-if-a-column-is-categorical-in-dataframes/43830/7 "2022-01-22T11:39:12Z")

</div>

As (almost) always, we can learn a lot from looking at the documentation:

[https://categoricalarrays.juliadata.org/stable/apiindex/#CategoricalArrays.isordered-Tuple{CategoricalArray}](https://categoricalarrays.juliadata.org/stable/apiindex/#CategoricalArrays.isordered-Tuple%7BCategoricalArray%7D)

```julia
isordered(A::CategoricalArray)

Test whether entries in A can be compared using <, > and similar operators, using the ordering of levels.

```
