📖 Scenario: You work in a small business that keeps sales records as text. You want to analyze sales by date, but the dates are stored as words, not real dates. You need to change these text dates into real date values so you can sort and filter them easily.
🎯 Goal: Convert a list of sales dates stored as strings into real date objects using pandas.to_datetime(). This will help you work with dates correctly in your analysis.
📋 What You'll Learn
Create a pandas DataFrame with a column of date strings
Create a variable to hold the date format string
Use pandas to_datetime() with the format string to convert the date strings
Print the DataFrame with the converted dates
💡 Why This Matters
🌍 Real World
Businesses often get dates as text from reports or user input. Converting these to real dates lets them sort, filter, and analyze data by time.
💼 Career
Data analysts and scientists frequently convert date strings to datetime objects to prepare data for time series analysis, reporting, and visualization.
Progress0 / 4 steps
1
Create a DataFrame with date strings
Create a pandas DataFrame called sales_data with one column named 'date' containing these exact strings: '2023-01-15', '2023-02-20', '2023-03-25'.
Data Analysis Python
Hint
Use pd.DataFrame and pass a dictionary with key 'date' and the list of strings as value.
2
Create a date format string
Create a variable called date_format and set it to the string '%Y-%m-%d' which matches the format of the date strings.
Data Analysis Python
Hint
The format '%Y-%m-%d' means 4-digit year, 2-digit month, 2-digit day separated by dashes.
3
Convert the date strings to datetime objects
Use pandas.to_datetime() with the sales_data['date'] column and the date_format variable to convert the strings to datetime objects. Save the result back to sales_data['date'].
Data Analysis Python
Hint
Use pd.to_datetime() with the format parameter set to date_format.
4
Print the DataFrame with converted dates
Print the sales_data DataFrame to show the dates as datetime objects.
Data Analysis Python
Hint
Use print(sales_data) to display the DataFrame.
Practice
(1/5)
1. What does the pd.to_datetime() function do in pandas?
easy
A. Calculates the difference between two dates
B. Converts datetime objects into strings
C. Converts strings or numbers into datetime objects
D. Sorts dates in ascending order
Solution
Step 1: Understand the function purpose
pd.to_datetime() is used to convert text or numbers that represent dates into pandas datetime objects.
Step 2: Identify the correct description
Among the options, only converting strings or numbers into datetime objects matches the function's purpose.
Final Answer:
Converts strings or numbers into datetime objects -> Option C
Quick Check:
to_datetime() = Converts to datetime [OK]
Hint: Remember: to_datetime() turns text into dates [OK]
Common Mistakes:
Thinking it converts dates back to strings
Confusing it with date difference calculation
Assuming it sorts dates automatically
2. Which of the following is the correct syntax to convert a column 'date_str' in DataFrame df to datetime using pandas?
easy
A. df['date_str'] = pd.to_datetime(df['date_str'])
B. df.to_datetime('date_str')
C. pd.to_datetime(df.date_str, inplace=true)
D. df['date_str'].to_datetime()
Solution
Step 1: Recall correct function usage
The correct way is to call pd.to_datetime() on the column and assign it back to the DataFrame column.
Step 2: Check each option
df['date_str'] = pd.to_datetime(df['date_str']) correctly uses pd.to_datetime(df['date_str']) and assigns it back. Others misuse syntax or methods.
Final Answer:
df['date_str'] = pd.to_datetime(df['date_str']) -> Option A
Quick Check:
Assign converted column back = df['date_str'] = pd.to_datetime(df['date_str']) [OK]
Hint: Use pd.to_datetime() and assign back to column [OK]
Common Mistakes:
Calling to_datetime() directly on DataFrame
Using inplace=true which is invalid here
Trying to call to_datetime() as a method on Series
A. The date format string does not match the input dates
B. pd.to_datetime() cannot convert strings with dashes
C. DataFrame creation syntax is incorrect
D. Missing dayfirst=True parameter
Solution
Step 1: Check input date format
The dates are '2023-31-12' and '2024-01-15', which look like year-day-month format.
Step 2: Compare with format string
The format string '%Y-%m-%d' expects year-month-day, so it does not match the input dates.
Final Answer:
The date format string does not match the input dates -> Option A
Quick Check:
Format string must match input date pattern [OK]
Hint: Match format string exactly to date string pattern [OK]
Common Mistakes:
Assuming default format works for all date strings
Using wrong format codes for day and month
Ignoring error messages about parsing failure
5. You have a DataFrame with a column 'date_str' containing dates in mixed formats like '2023/12/31', '31-01-2024', and 'Feb 15, 2024'. How can you convert this column to datetime correctly?
hard
A. Manually split and parse each date format before conversion
B. Use pd.to_datetime(df['date_str'], errors='coerce') without format to parse mixed formats
C. Use df['date_str'].astype('datetime64') to convert directly
D. Use pd.to_datetime(df['date_str'], format='%Y/%m/%d') to parse all dates
Solution
Step 1: Understand mixed date formats challenge
Mixed formats cannot be parsed with a single fixed format string.
Step 2: Use flexible parsing with errors='coerce'
Calling pd.to_datetime() without a format lets pandas try multiple formats. Using errors='coerce' converts unparseable dates to NaT safely.
Step 3: Avoid rigid format or manual splitting
Using a fixed format or manual splitting is inefficient and error-prone.
Final Answer:
Use pd.to_datetime(df['date_str'], errors='coerce') without format to parse mixed formats -> Option B
Quick Check:
Flexible parsing with errors='coerce' handles mixed dates [OK]
Hint: Omit format and use errors='coerce' for mixed date formats [OK]
Common Mistakes:
Forcing a single format on mixed date strings
Using astype('datetime64') which fails on strings
Trying to manually parse each format separately
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.