Pandas Convert Datetime to Date Column

  • Post author:
  • Post category:Pandas / Python
  • Post last modified:January 30, 2023

You can convert DateTime to Date in pandas by using dt.date and dt.normalize() methods. In Pandas, DateTime is a collection of date and time 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.

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.


# Below are quick example

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

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

# 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 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(df)

Yields below output.


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

2. Convert Datetime to Date

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


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

Yields below output.


# Output:
   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

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


# Using single date function
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 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(df)

Yields same output as above.

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)

Conclusion

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

Happy Learning !!

References

Malli

I am Mallikarjuna an experienced technical writer with a passion for translating complex Python concepts into clear, concise, and user-friendly documentation. Over the years, I have written hundreds of articles in Pandas, NumPy, Python, and I take pride in my ability to bridge the gap between technical experts and end-users by delivering well-structured, accessible, and informative content.

Leave a Reply

You are currently viewing Pandas Convert Datetime to Date Column