w3resource

Validating date formats in a Pandas DataFrame


Pandas: Data Validation Exercise-9 with Solution


Write a Pandas program to validate date formats in a DataFrame.

The following exercise demonstrates how to validate that a column contains correctly formatted date values using to_datetime().

Sample Solution :

Code :

import pandas as pd

# Create a sample DataFrame with date strings
df = pd.DataFrame({
    'Event': ['Conference', 'Meeting', 'Seminar'],
    'Date': ['2020-05-01', '2020-06-15', 'Invalid-Date']
})

# Validate the 'Date' column by converting it to datetime
df['Valid_Date'] = pd.to_datetime(df['Date'], errors='coerce')

# Output the result
print(df)

Output:

        Event          Date Valid_Date
0  Conference    2020-05-01 2020-05-01
1     Meeting    2020-06-15 2020-06-15
2     Seminar  Invalid-Date        NaT

Explanation:

  • Created a DataFrame with date strings, including an invalid date.
  • Used to_datetime() to validate the 'Date' column, converting invalid dates to NaT.
  • Displayed the DataFrame with a new 'Valid_Date' column that contains validated dates.

Python-Pandas Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.