• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:17 mins read
You are currently viewing Pandas Convert Datetime to Date Column

You can convert DateTime to Date in pandas by using pandas.Series.dt.date and dt.normalize() methods. In Pandas, DateTime is a collection of dates and times in the format of “YYYY-MM-DD HH:MM:SS” where YYYY-MM-DD is referred to as the date and HH:MM:SS is referred to as Time.

In this article, I will cover how to convert DateTime to date by extracting only date from the date and time column of DataFrame.

Related: You can also convert datetime to seconds from the DataFrame.

1. Quick Examples of Pandas Convert Datetime to Date

If you are in a hurry, below are some quick examples of how to convert the DateTime column to Date.


# Quick examples of convert Datetime to Date

# Example 1: Using Date function                               
df['Date'] = pd.to_datetime(df["InsertedDateTime"]).dt.date

# Example 2: Using single date function
Datetime = '2021-11-15 21:04:15'
df2 = pd.to_datetime(Datetime).date()

# Example 3: Using normalize() method
df['Date'] = pd.to_datetime(df["InsertedDateTime"]).dt.normalize()

Now, let’s create a DataFrame with a few rows and columns, execute these examples, and validate the results. Our DataFrame contains column names CoursesFee and InsertedDateTime.


# Create Pandas DataFrame
import pandas as pd
technologies = ({
         'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
         'Fee':[22000,25000,23000,24000,26000],
         "InsertedDateTime":['2021-11-15 21:04:15','2020-05-04 22:04:10','2018-01-26 15:23:14','2019-02-18 10:05:18','2021-12-10 15:13:21']
           })
df = pd.DataFrame(technologies)
print("Create DataFrame:\n", df)

Yields below output.

pandas convert Datetime date

2. Convert Datetime to Date

The Datetime column can store the date and time together, If you want just the date and do not want time in format YYYY-MM-DD format use pandas.Series.dt.date. Here, pandas.to_datetime() is used to convert the String column to DateTime.


# Using Date function
df['Date'] = pd.to_datetime(df["InsertedDateTime"]).dt.date
print("After converting datetime to date:\n", df)

Yields below output.

pandas convert Datetime date

If the date in the string column is already in date format, then just use date() to truncate the time.


# Using single date function to get
# date from datetime column
Datetime = '2021-11-15 21:04:15'
df2 = pd.to_datetime(Datetime).date()
print(df2)

# Output:
# 2021-11-15

3. Convert Datetime to Date Using normalize() Method

You can convert DateTime to date using the normalize() method. Use pd.to_datetime(df["InsertedDateTime"]).dt.normalize() method to normalize the data by extracting the date from DateTime.


# Using normalize() method
df['Date'] = pd.to_datetime(df["InsertedDateTime"]).dt.normalize()
print("After converting datetime to date:\n", df)

Yields below output.


# Output:
# After converting datetime to date:
    Courses    Fee     InsertedDateTime       Date
0    Spark  22000  2021-11-15 21:04:15 2021-11-15
1  PySpark  25000  2020-05-04 22:04:10 2020-05-04
2   Hadoop  23000  2018-01-26 15:23:14 2018-01-26
3   Python  24000  2019-02-18 10:05:18 2019-02-18
4   Pandas  26000  2021-12-10 15:13:21 2021-12-10

5. Complete Example Of Convert Datetime to Date


# Example Of Convert Datetime to Date 
import pandas as pd
technologies = ({
         'Courses':["Spark","PySpark","Hadoop","Python","Pandas"],
         'Fee':[22000,25000,23000,24000,26000],
         "InsertedDateTime":['2021-11-15 21:04:15','2020-05-04 22:04:10','2018-01-26 15:23:14','2019-02-18 10:05:18','2021-12-10 15:13:21']
           })
df = pd.DataFrame(technologies)
print(df)                               

# Using Date function                               
df['Date'] = pd.to_datetime(df["InsertedDateTime"]).dt.date
print(df)

# Using single date function
Datetime = '2021-11-15 21:04:15'
df2 = pd.to_datetime(Datetime).date()
print(df2)

# Using normalize() method
df['Date'] = pd.to_datetime(df["InsertedDateTime"]).dt.normalize()
print(df)

Frequently Asked Questions on Convert Datetime to Date

How can I convert a column of datetime objects to date objects in a Pandas DataFrame?

If you have a column of datetime objects in a Pandas DataFrame and you want to convert it to date objects, you can use the dt.date accessor. For example, df['datetime_column'].dt.date is used to extract the date portion of the datetime objects in the ‘datetime_column’, and a new column ‘date_column’ is created to store the date objects.

How do I convert a datetime column to a date column in a Pandas DataFrame?

To convert a datetime column to a date column in a Pandas DataFrame, you can use the dt accessor along with the date attribute. For example, the pd.to_datetime function is used to convert the ‘datetime_column’ to a datetime data type. Then, the dt.date attribute is used to extract the date part and create a new ‘date_column’. Adjust the column names based on your actual DataFrame.

What if my datetime column is in a string format?

If your datetime column is in a string format, you can use the pd.to_datetime function to convert it to a datetime object before applying the dt.date accessor.

How do I set the date column as the index of the DataFrame?

To set the date column as the index of the DataFrame in Pandas, you can use the set_index method. For example, the set_index method is used to set the ‘date_column’ as the index of the DataFrame. The inplace=True parameter modifies the DataFrame in place without the need to create a new variable.

Is there a way to convert datetime to date without creating a new column?

You can convert a datetime column to a date without creating a new column by directly overwriting the existing datetime column. For example, the original ‘datetime_column’ is directly overwritten with the date values using df['datetime_column'] = df['datetime_column'].dt.date. This modifies the DataFrame in place, and the ‘datetime_column’ now contains date values.

What if my datetime column has a different format?

If your datetime column has a different format, you may need to specify the format when using the pd.to_datetime function. For example, the format parameter in pd.to_datetime is used to specify the format of the datetime strings in the ‘datetime_column’. Adjust the format string ('%d-%b-%Y %H:%M:%S' in this case) based on the actual format of your datetime column.

Conclusion

In this article, you have learned how to convert DateTime to date in pandas by using pandas.Series.dt.date and dt.normalize(), and date() methods and with more examples.

Happy Learning !!

References

Malli

Malli is an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly articles. Over the years, he has written hundreds of articles in Pandas, NumPy, Python, and takes pride in ability to bridge the gap between technical experts and end-users.

Leave a Reply