w3resource

Validating date formats in a Pandas DataFrame


9. Validating Date Formats in a DataFrame

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.

For more Practice: Solve these Related Problems:

  • Write a Pandas program to validate the date format in a specific column and output rows with incorrectly formatted dates.
  • Write a Pandas program to convert a column to datetime format and report any parsing errors.
  • Write a Pandas program to check that dates in a column conform to a specified format and flag non-conforming entries.
  • Write a Pandas program to validate date formats and split a datetime column into separate date and time columns.

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.