check if a column is of datetime dtype in pandas

Check if a DataFrame column is of datetime dtype in Pandas

In this tutorial, we will learn how to check if the dtype of a column in a Pandas dataframe is datetime. Good exposure to Python is recommended but not required.

How to check if a dataframe’s column is of datetime type?

Pandas comes with a is_datetime64_any_dtype() function that checks if the dtype of a dataframe column is datetime or not. The function takes a Pandas Series as input and returns True if the Series object has a datetime dtype; else it returns False. To check if a column has a dtype datetime, the syntax is-

is_column_datetime_type = is_datetime64_any_dtype(df[column])

Here,

  • df — A Pandas DataFrame object.
  • df[column] — a dataframe property to get a column.
  • is_datetime64_any_dtype(df[column]) — returns True if the column has dtype datetime; else it returns False.

Here, we have passed a column to the above function to check if it is of datetime type. Note that we have passed the column using df[column] which returns a column as a Pandas Series object.

Another way to check if the column has a datetime dtype is to compare the dtype of the column with a dtype of an object that we already know has a datetime dtype. The syntax for this is-

is_datetime_column = (df[column].dtype == pd.date_range(start, end).dtype)

Here,

pd.date_range(start, end) —a function which takes start and end values and creates a DatetimeIndex object for the range of dates.

Here, we created a dummy range of dates using pd.date_range(). This function returns a DatetimeIndex object which has a datetime dtype. We compared the dtypes of this object with the dtype of our column to check if they are the same. If they are the same, the comparison will return True; otherwise, it will return False

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

Examples

Let’s understand the above syntax with some examples. For our example, we will create a dataframe for restaurant sales. For simplicity, we will restrict the entries to five.

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

d = {
    "Cost": [419, 369, 199, 359, 689],
    "Service Charges" : [20.05, 18.45, 9.95, 17.55, 34.50],
    "Time Stamp" : pd.date_range(start="2022/10/08 09:00:00", end="2022/10/08 23:00:00", periods=5)
    }

df = pd.DataFrame(d)
print(df)

Output:

   Cost  Service Charges          Time Stamp
0   419            20.05 2022-10-08 09:00:00
1   369            18.45 2022-10-08 12:30:00
2   199             9.95 2022-10-08 16:00:00
3   359            17.55 2022-10-08 19:30:00
4   689            34.50 2022-10-08 23:00:00

Here, the last column, i.e., ‘Time Stamp’, is of dtype datetime, while the other two have int and float dtypes. In the examples below, we will use the Pandas function to check if the dtype of the columns is datetime.

Example 1: Check if the column has dtype datetime using function is_datetime64_any_dtype()

Let’s first pass the ‘Time Stamp’ column of the dataframe df to see if it has datetime dtype.

is_datetime_column = is_datetime64_any_dtype(df["Time Stamp"])
print(is_datetime_column)

Output:

True

The function returns True, which means the column is of type datetime. Now for a column without datetime dtype, the function must return False.

is_datetime_column = is_datetime64_any_dtype(df["Cost"])
print(is_datetime_column)

Output:

False

As you can see the output is False, as we expected earlier.

Example 2: Use dtypes comparison to check if the column has datetime dtype

Now, we will compare the dtype of a column with the dtype of a Pandas date range. Since the date range has a datetime dtype, the result of the comparison will tell us if the column has a datetime dtype.

is_datetime_column = (df['Time Stamp'].dtype == pd.date_range(1, 2).dtype)
print(is_datetime_column)

Output:

True

Here, the column was of dtype datetime, hence the result is True. Let’s also see the result if the column is of a different dtype.

is_datetime_column = (df['Cost'].dtype == pd.date_range(1, 2).dtype)
print(is_datetime_column)

Output:

False

Since the column didn’t have datetime dtype, the comparison returned False, as was expected.

Summary

In this tutorial, we looked at how to:

  • Use the Pandas is_datetime64_any_dtype() function to determine whether the dataframe column has a datetime dtype.
  • Use dtype comparison to see if the column’s dtype is datetime. 

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