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 Fals
e. 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])
— returnsTrue
if the column has dtype datetime; else it returnsFalse
.
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
.
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.
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 –
- Pandas – How to Create a date range?
- Pandas – Get only date from datetime
- Pandas – Get Day of Week from Date
- Pandas – Change Format of Date Column
- Pandas – Extract Year from a datetime column
- Get Quarter from Date in Pandas
- Remove Time from Date in Pandas
- Pandas – Category Column with Datetime Values
- Pandas – Get Month from Date
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.