Challenge - 5 Problems
Date Parsing Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Parsing mixed date formats with to_datetime()
What is the output of this code snippet when parsing mixed date formats using pandas to_datetime()?
Pandas
import pandas as pd dates = ['2023-01-01', '01/02/2023', 'March 3, 2023', '2023.04.04'] df = pd.DataFrame({'date_str': dates}) df['parsed'] = pd.to_datetime(df['date_str']) print(df['parsed'].dt.month.tolist())
Attempts:
2 left
💡 Hint
Remember that to_datetime() can parse many common date formats automatically.
✗ Incorrect
The dates are parsed correctly into datetime objects. '01/02/2023' is interpreted as January 2, 2023 by default (month/day/year). The months extracted are 1, 2, 3, and 4 respectively.
❓ data_output
intermediate2:00remaining
Handling errors with to_datetime()
Given this code, what will be the content of the 'parsed' column after running to_datetime() with errors='coerce'?
Pandas
import pandas as pd dates = ['2023-01-01', 'not a date', '2023-03-15'] df = pd.DataFrame({'date_str': dates}) df['parsed'] = pd.to_datetime(df['date_str'], errors='coerce') print(df['parsed'].tolist())
Attempts:
2 left
💡 Hint
errors='coerce' replaces invalid parsing with NaT (Not a Time).
✗ Incorrect
The invalid string 'not a date' cannot be parsed, so it becomes NaT. Valid dates are converted to Timestamps.
❓ visualization
advanced3:00remaining
Visualizing parsed dates by year
After parsing these date strings with to_datetime(), which bar chart correctly shows the count of dates per year?
Pandas
import pandas as pd import matplotlib.pyplot as plt dates = ['2022-12-31', '2023-01-01', '2023-06-15', '2024-01-01'] df = pd.DataFrame({'date_str': dates}) df['parsed'] = pd.to_datetime(df['date_str']) year_counts = df['parsed'].dt.year.value_counts().sort_index() year_counts.plot(kind='bar') plt.show()
Attempts:
2 left
💡 Hint
Count how many dates fall in each year after parsing.
✗ Incorrect
There is one date in 2022, two in 2023, and one in 2024. The bar chart reflects these counts.
🧠 Conceptual
advanced2:00remaining
Understanding dayfirst parameter in to_datetime()
What is the output of this code when parsing '01/02/2023' with dayfirst=True?
Pandas
import pandas as pd date_str = '01/02/2023' parsed_date = pd.to_datetime(date_str, dayfirst=True) print(parsed_date.strftime('%Y-%m-%d'))
Attempts:
2 left
💡 Hint
dayfirst=True means the first number is the day, not the month.
✗ Incorrect
With dayfirst=True, '01/02/2023' is interpreted as 1st February 2023.
🔧 Debug
expert3:00remaining
Why does this to_datetime() call raise an error?
Consider this code snippet. Why does it raise a ValueError?
Pandas
import pandas as pd dates = ['2023-01-01', '2023-02-30'] df = pd.DataFrame({'date_str': dates}) df['parsed'] = pd.to_datetime(df['date_str'], format='%Y-%m-%d')
Attempts:
2 left
💡 Hint
Check if all dates are valid calendar dates.
✗ Incorrect
February 30th does not exist. Using format parameter enforces exact matching and raises ValueError on invalid dates.