Parse Date Formats Practice Problem
This data science coding problem helps you practice Data Type Conversion & Validation, parse date formats, and implementation skills. Read the problem statement, write your solution, and strengthen your understanding of Data Type Conversion & Validation.
- Problem ID: 40
- Problem key: 40-parse-date-formats
- URL: https://datacrack.app/solve/40-parse-date-formats
- Difficulty: easy
- Topic: Data Type Conversion & Validation
- Module: Data Cleaning
Problem Statement
# Parse Date Formats
### 🎯 Goal
Parse date strings from various formats into a standardized ISO format (`YYYY-MM-DD`).
### 💻 Task
Implement `parse_dates(data, column)` that:
1. Converts the input dictionary to a DataFrame
2. Parses the specified column using `pd.to_datetime` (auto-detecting the format)
3. Converts the parsed dates to ISO format strings (`YYYY-MM-DD`)
4. Returns the resulting DataFrame
---
### 📥 Input
- `data`: A dictionary where keys are column names and values are lists of date strings
- `column`: The name of the column containing date strings
### 📤 Output
- A pandas DataFrame with the date column converted to ISO format strings (`YYYY-MM-DD`)
---
### 🧩 Starter Code
```python
import pandas as pd
import numpy as np
def parse_dates(data, column):
"""
Parse date strings from various formats into ISO format.
Args:
data (dict): Input data as dictionary with date strings
column (str): Name of the column containing dates
Returns:
pd.DataFrame: DataFrame with dates as ISO format strings
"""
# TODO: Convert the input dictionary to a DataFrame
# TODO: Use pd.to_datetime to parse the date column
# TODO: Convert parsed dates to ISO format strings (YYYY-MM-DD)
# TODO: Return the resulting DataFrame
pass
```
---
### 💡 Examples
**Example 1:** ISO format input (already standard)
```python
data = {"date": ["2024-01-15", "2024-03-20", "2024-06-10"]}
parse_dates(data, "date")
```
```
date
0 2024-01-15
1 2024-03-20
2 2024-06-10
```
**Example 2:** European day/month/year format
```python
data = {"date": ["15/01/2024", "20/03/2024", "10/06/2024"]}
parse_dates(data, "date")
```
```
date
0 2024-01-15
1 2024-03-20
2 2024-06-10
```
**Example 3:** Month name format
```python
data = {"date": ["Jan 15, 2024", "Mar 20, 2024", "Jun 10, 2024"]}
parse_dates(data, "date")
```
```
date
0 2024-01-15
1 2024-03-20
2 2024-06-10
```Starter Code
import pandas as pd
import numpy as np
def parse_dates(data, column):
"""
Parse date strings from various formats into ISO format.
Args:
data (dict): Input data as dictionary with date strings
column (str): Name of the column containing dates
Returns:
pd.DataFrame: DataFrame with dates as ISO format strings
"""
# TODO: Convert the input dictionary to a DataFrame
# TODO: Use pd.to_datetime to parse the date column
# TODO: Convert parsed dates to ISO format strings (YYYY-MM-DD)
# TODO: Return the resulting DataFrame
pass