get only date from datetime values in a pandas dataframe

Pandas – Get only date from datetime

In this tutorial, we’ll try to solve 2 problems:

  1. Get only the date from a specific datetime type cell in a pandas dataframe.
  2. Get dates from all the cells of a pandas datetime type column.

Datatype conversion:

Before going through the below solutions for the problem, we first need to make sure that the datatype of the date_column for which we need the date has to be pandas datetime. In case you have strings in the column then try to use pandas.to_datetime method to convert the datatype of the column to pandas datetime.

code:

df['date_column'] = pd.to_datetime(df['date_column'])

The above line of code changes the datatype of the ‘date_column’ column to pandas datetime.

Problem – 1: Extract only date from a specific cell of dataframe

Here, we’re trying to extract the date from a specific cell of a pandas dataframe object. To achieve this we need to follow the below steps.

  1. Make sure that the datatype of the column is pandas datetime. If not, then use the above step specified in Datatype conversion section
  2. Once, you make sure the datatype of the column is pandas datetime, then select the specific cell which you need to extract the date from.
  3. Then use the .date() method over the selected cell which gives the date of the specified cell.

Basic Syntax:

df['date_column'] = pd.to_datetime(df['date_column'])
df['date_column'][0].date()

The above code first converts the data type of the column to pandas datetime and then extracts the date of the specified cell using the .date() method which is an inbuilt method for the timestamp datatype.

Examples

Now, let us have a look at some of the examples to demonstrate the above solution.

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

Note:
In case you don’t have any dataframe to try the solution, use the below implementation to create a dataframe with some dates as cells. The below-given dataframe will be used to demonstrate all the examples below.

import pandas as pd
dates = {'date_column':['14 - 05 - 2017 1:30pm', '2017 3:40am', '07 - 09 - 2019 4:45pm']}
df = pd.DataFrame(dates)
df['date_column'] = pd.to_datetime(df['date_column'], dayfirst = True)
df

Output:

pandas datetime column with both date and time information

Example – 1

code:

df['date_column'][0].date()

Output:

datetime.date(2017, 5, 14)

Example – 2

code:

df['date_column'][2].date()

Output:

datetime.date(2019, 9, 7)

Problem – 2: Extract date from all the cells in the column of a dataframe

get only date from datetime values in a pandas dataframe

Here, we’re trying to extract the date from all the cells in the column of a pandas dataframe object. To achieve this we need to follow the below steps.

  1. Make sure that the datatype of the column is pandas datetime. If not, then use the above step specified in Datatype conversion section
  2. Once you make sure the datatype of the column is pandas datetime then select the specific column cell which you need to extract the date from.
  3. Then use the .dt.date attribute over the selected column which gives the dates of the all cells in the column.

Basic Syntax:

df['date_column'] = pd.to_datetime(df['date_column'])
df['date_column'].dt.date

In the above code, we first convert the data type of the column to pandas datetime and then extract the date of all the cells of the column using the .dt.date attribute.

Examples

Now, let us have a look at some of the examples to demonstrate the above solution.

Note:
In case you don’t have any dataframe to try the solution, use the below implementation to create a dataframe with some dates as cells. The below-given dataframe will be used to demonstrate all the below examples.

import pandas as pd
dates = {'date_column':['14 - 05 - 2017 1:30pm', '2017 3:40am', '07 - 09 - 2019 4:45pm']}
df = pd.DataFrame(dates)
df['date_column'] = pd.to_datetime(df['date_column'], dayfirst = True)
df

Output:

pandas datetime column with both date and time information

Example – 1

code:

df['date_column'].dt.date

Output:

0    2017-05-14
1    2017-01-01
2    2019-09-07
Name: date_column, dtype: object

Summary

In this we looked at 2 different problems and tried to solve each one separately

  1. Extract only the date from a datetime type cell in a pandas dataframe.

    Solution: Using .date() method

    Result: date of the datetime object

  2. Extract only the dates from all the cells in a pandas datetime type column.

    Solution: Using .dt.date attribute

    Result: Series of dates of all cells from the specific column

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

  • Chaitanya Betha

    I'm an undergrad student at IIT Madras interested in exploring new technologies. I have worked on various projects related to Data science, Machine learning & Neural Networks, including image classification using Convolutional Neural Networks, Stock prediction using Recurrent Neural Networks, and many more machine learning model training. I write blog articles in which I would try to provide a complete guide on a particular topic and try to cover as many different examples as possible with all the edge cases to understand the topic better and have a complete glance over the topic.

Scroll to Top