In this tutorial, we will look at how to check if a pandas series with category dtype is ordered or not with the help of some examples.

## Ordered and unordered categorical values

A categorical field may or may not be ordered. For example, gender values “M” and “F” do not have an order to them and can be considered as an unordered categorical field.

Some categorical fields on the other hand are ordered. For example, t-shirt sizes, S, M, L, and XL. These values are categorical but they also have an order to them, S < M < L < XL.

The categorical data type in pandas is used to store categorical data. It also allows you to specify an order to the values (if any).

## How to check if a pandas categorical data is ordered or not?

You can use the `ordered`

property of a Pandas categorical data to check if the categories in the data are ordered or not. The following is the syntax –

# s is pandas series with category dtype s.cat.ordered

It returns a boolean value representing whether the given categorical data is ordered or not.

## Examples

Let’s look at some examples of checking for order in Pandas categorical data.

import pandas as pd # create a pandas series with category dtype shirt_size = pd.Series(["M", "S", "L", "M", "XL"], dtype="category") # check if the category is ordered print(shirt_size.cat.ordered)

Output:

**Data Science Programs By Skill Level**

**Introductory** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 Find Data Science Programs 👨💻 111,889 already enrolled**

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

False

Here, we create a Pandas series with `category`

dtype. We then check if it’s ordered or not by accessing its `ordered`

property. You can see that we get `False`

as the output. The category is unordered by default since we didn’t specify it to be ordered during creation.

Let’s now look at another example. Here let’s create a pandas category that is ordered using the `CategoricalDtype`

and then check whether it’s ordered or not.

from pandas.api.types import CategoricalDtype # create an ordered category type for shirt size cat_type = CategoricalDtype(categories=["S", "M", "L", "XL"], ordered=True) # create a pandas series of shirt sizes shirt_size = pd.Series(["M", "S", "L", "M", "XL"]) # change the series type to the custom category type shirt_size = shirt_size.astype(cat_type) # check if category is ordered print(shirt_size.cat.ordered)

Output:

True

You can see that we get `True`

as the output because the category is ordered. If you display the series, you can see the category values and their order.

# display the series print(shirt_size)

Output:

0 M 1 S 2 L 3 M 4 XL dtype: category Categories (4, object): ['S' < 'M' < 'L' < 'XL']

We get the order ‘S’ < ‘M’ < ‘L’ < ‘XL’ of the categories in the series.

You might also be interested in –

**Subscribe to our newsletter for more informative guides and tutorials. ****We do not spam and you can opt out any time.**