Python - Filter the columns in a pandas dataframe based on whether they are of type date or not

Given a Pandas DataFrame, we have to filter its columns based on whether they are of type date or not. By Pranit Sharma Last updated : September 27, 2023

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Datetime is a library in python which is a collection of date and time. Inside Datetime, we can access date and time in any format, but usually, date is present in the format of "yy-mm-dd" and time is present in the format of "HH:MM:SS".

Here,

  • yy means year
  • mm means month
  • dd means day
  • HH means hours
  • MM means minutes
  • SS means seconds

The datetime data type is that data type that has a proper time and date format, here in this article, we are going to learn how to check if a column value has datetime data type or any other data type?

Problem statement

Given a Pandas DataFrame, we have to filter its columns based on whether they are of type date or not.

Filtering the columns in a dataframe based on whether they are of type date or not

Suppose we have a dataframe consisting of a column that has a date in string format, we will convert the string into datettime format with the help of pd.to_datetime() and then we will check if this column has dtype as datetime or not using df.col.dtype() which will return the data type of this column.

Let us understand with the help of an example,

Python program to filter the columns in a dataframe based on whether they are of type date or not

# Importing pandas package
import pandas as pd
import numpy as np

# Creating dictionary
d = {
    'Date':['07/12/2001','04/04/2001','12/04/2001','11/08/2001'],
    'Numerical':[20,21,21,21]
}

# Creating DataFrame
df = pd.DataFrame(d)

# Display Original DataFrames
print("Created DataFrame:\n",df,"\n")

# convert the dtype of date column
df['Date'] = pd.to_datetime(df['Date'])

# Now check the dtype of Date column
result = df.Date.dtype

# Display the data type of this column
print("Data type:\n",result)

# Now printing column have datetimn type values
print("column have datetimn type values...")
result = df.select_dtypes(include=[np.datetime64])
print(result)

Output

The output of the above program is:

Created DataFrame:
          Date  Numerical
0  07/12/2001         20
1  04/04/2001         21
2  12/04/2001         21
3  11/08/2001         21 

Data type:
 datetime64[ns]
column have datetimn type values...
        Date
0 2001-07-12
1 2001-04-04
2 2001-12-04
3 2001-11-08

Python Pandas Programs »

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.