check if a pandas dataframe column is of object dtype

Check if Pandas DataFrame column has object dtype

In this tutorial, you will learn to check if a column has object dtype. It is recommended that you have good exposure to Python, but this is not necessary.

How to check if a Pandas DataFrame column has an object dtype?

check if a pandas dataframe column is of object dtype

To check if a Pandas DataFrame column has an object dtype, we will use Pandas built-in is_object_dtype() function. The following is the syntax-

is_col_object_dtype = is_object_dtype(df[column])

Here,

  • is_object_dtype() – function to check if a Pandas Series has object dtype.
  • df – a Pandas DataFrame.
  • df[column] – dataframe’s column.
    df[column] is a Pandas Series representing the column. When we pass it to the function is_object_dtype(), the function returns True if this column has a dtype object; else it returns False.

Another way to do this is by using the equality operator, ==. The syntax is –

is_col_object_dtype = (df[column].dtype == object)

Here,

  • df – a Pandas DataFrame.
  • df[column] – Returns dataFrame column as Pandas Series.
  • df[column].dtype – Returns the dtype of the column.
  • object – An inbuilt base class in Python from which all the base classes are built.

Here, df[column].dtype returns a numpy.dtype object. This contains the dtype of the column. NumPy allows you to compare the dtypes of objects with the type identifiers of Python. We used this ability to compare the dtype of the column with the object identifier.

Examples

Let’s use some examples to understand the above syntax. We will create a dataframe for a restaurant’s sales as an example. To keep it simple, we only used five entries.

import pandas as pd
from pandas.api.types import is_object_dtype

#Create a dictionary to make a dataframe
d = {
    "Item" : ["Pulao", "Masala Dosa", "Idli", "Onion Dosa", "Thali"],
    "Cost": [419, 369, 199, 359, 689],
    "Service Charges" : [20.05, 18.45, 9.95, 17.55, 34.50],
    }

#Create the dataframe from dictionary 'd'
df = pd.DataFrame(d)

#Print the dataframe
print(df)

Output:

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

          Item  Cost  Service Charges
0        Pulao   419            20.05
1  Masala Dosa   369            18.45
2         Idli   199             9.95
3   Onion Dosa   359            17.55
4        Thali   689            34.50

In the above output, we can see that the dataframe has been created. As you will see later, the ‘Cost’ and the ‘Service Charges’ columns have int and float dtypes respectively, whereas the ‘Item’ column has an object dtype. By default, all the Pandas columns having at least one non-numeric or non-datetime type cell have a dtype as object.

Example 1: Check if the dataframe column has object dtype using Pandas inbuilt function

To check if the column has a datetime dtype, pass the column to the Pandas function is_object_dtype().

is_col_object_dtype = is_object_dtype(df["Item"])
print(is_col_object_dtype)

Output:

True

The result is True, indicating that the column has a dtype object. If the column doesn’t have a dtype object, then the result will be False. Let’s pass the ‘Cost’ column, which has an int dtype, to the function.

is_col_object_dtype = is_object_dtype(df["Cost"])
print(is_col_object_dtype)

Output:

False

As expected, we get False as result.

Example 2: Check if the dataframe column has object dtype using the equality operator

Instead of using Pandas’ built-in function, let’s use the equality operator to check the dtype of the item column is object or not. 

is_col_object_dtype = (df["Item"].dtype == object)
print(is_col_object_dtype)

Output:

True

The output is the same as that returned by the function is_object_dtype(). Let’s check the ‘Service Charges’ column. Note that this column has a float dtype.

is_col_object_dtype = (df["Service Charges"].dtype == object)
print(is_col_object_dtype)

Output:

False

Thus, we see that the comparison of dtypes also yields the same results as that of Pandas built in function.

Summary

From this tutorial, we looked at:

  • Use Pandas’ inbuilt function is_object_dtype() to check if the column has an object dtype.
  • Use Pandas column’s dtype attribute and Python’s equality operator, == to check if the column has an object dtype.

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

Scroll to Top