category column with datetime values in pandas

Pandas – Category Column with Datetime Values

In this tutorial, we will look at how to convert a datetime type column to a category type column in Pandas and apply common datetime operations.

How to convert datetime column to category type column in Pandas?

You can use the Pandas astype() function to change the type of a column. To convert a column to category dtype, pass “category” as an argument. The following is the syntax –

df["Col"] = df["Col"].astype("category")

It converts the column to category type. You can still access common datetime attributes (like month, year, etc.) and functions with the help of the .dt accessor.

Examples

Let’s look at some examples of converting a datetime column to a categorical column in Pandas. First, we’ll create a sample dataframe that we will be using throughout this tutorial.

import pandas as pd

# create a dataframe
df = pd.DataFrame({
        "Name": ["Tim", "Sarah", "Hasan", "Jyoti", "Jack"],
        "DOB": ["1999-04-02", "2001-12-23", "1998-07-13", "2000-01-01", "2001-08-29"]
})
# convert "DOB" to datetime
df["DOB"] = pd.to_datetime(df["DOB"])
# display the dataframe
print(df)

Output:

    Name        DOB
0    Tim 1999-04-02
1  Sarah 2001-12-23
2  Hasan 1998-07-13
3  Jyoti 2000-01-01
4   Jack 2001-08-29

We now have a dataframe containing the name and the date of birth of some students enrolled in a university.

Note that the “DOB” column is a datetime column.

Let’s change the type of the “DOB” column to category with the help of the astype() function.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 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.

# convert "DOB" to category type
df["DOB"] = df["DOB"].astype("category")
# display the column
print(df["DOB"])

Output:

0   1999-04-02
1   2001-12-23
2   1998-07-13
3   2000-01-01
4   2001-08-29
Name: DOB, dtype: category
Categories (5, datetime64[ns]): [1998-07-13, 1999-04-02, 2000-01-01, 2001-08-29, 2001-12-23]

You can see that the “DOB” column is now of category type.

Accessing datetime attributes on a category column with datetime values

You can use the .dt accessor on Pandas category columns with datetime values to access common datetime attributes.

For example, let’s get the year from the “DOB” column. For this, use the year attribute with the help of the .dt accessor.

# get year for DOB
print(df["DOB"].dt.year)

Output:

0    1999
1    2001
2    1998
3    2000
4    2001
Name: DOB, dtype: int64

You can see that we get the year for all the dates in the “DOB” column. Note that the data type of the resulting series is integer and not category.

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.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top