Bird
Raised Fist0
Data Analysis Pythondata~20 mins

to_datetime() conversion in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
to_datetime Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this to_datetime conversion?
Given the following code, what will be the output of print(df['date'])?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'date': ['2023-01-15', '2023/02/20', 'March 3, 2023']})
df['date'] = pd.to_datetime(df['date'])
print(df['date'])
A
0   2023-01-15 00:00:00
1   2023-02-20 00:00:00
2   2023-03-03 00:00:00
Name: date, dtype: datetime64[ns]
B
0   2023-01-15
1   2023-02-20
2   2023-03-03
Name: date, dtype: datetime64[ns]
C
0   2023-01-15 00:00
1   2023-02-20 00:00
2   2023-03-03 00:00
Name: date, dtype: datetime64[ns]
D
0   2023-01-15T00:00:00
1   2023-02-20T00:00:00
2   2023-03-03T00:00:00
Name: date, dtype: datetime64[ns]
Attempts:
2 left
💡 Hint
The default time added by to_datetime when only date is given is midnight with seconds.
❓ data_output
intermediate
1:30remaining
How many rows have valid datetime after conversion?
Consider this DataFrame and conversion. How many rows have valid datetime (not NaT) after conversion?
Data Analysis Python
import pandas as pd

df = pd.DataFrame({'date': ['2023-01-01', 'invalid_date', '2023-03-15']})
df['date'] = pd.to_datetime(df['date'], errors='coerce')
valid_count = df['date'].notna().sum()
print(valid_count)
A1
B0
C2
D3
Attempts:
2 left
💡 Hint
Invalid dates become NaT with errors='coerce'.
🔧 Debug
advanced
2:00remaining
What error does this to_datetime code raise?
What error will this code raise when run?
Data Analysis Python
import pandas as pd

dates = ['2023-01-01', '2023-02-30']
df = pd.DataFrame({'date': dates})
df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d', errors='raise')
AKeyError: 'date'
BValueError: day is out of range for month
CTypeError: unsupported operand type(s)
DNo error, conversion succeeds
Attempts:
2 left
💡 Hint
February 30th is not a valid date.
🚀 Application
advanced
2:30remaining
Which option converts mixed date formats correctly?
You have a column with dates in formats like '2023-01-01', '01/02/2023', and 'March 3, 2023'. Which code correctly converts all to datetime?
Apd.to_datetime(df['date'], infer_datetime_format=True, errors='coerce')
Bpd.to_datetime(df['date'], format='%m/%d/%Y')
Cpd.to_datetime(df['date'], format='%B %d, %Y')
Dpd.to_datetime(df['date'], format='%Y-%m-%d')
Attempts:
2 left
💡 Hint
Use infer_datetime_format to handle multiple formats.
🧠 Conceptual
expert
1:30remaining
What is the dtype of a Series after to_datetime conversion with utc=True?
If you convert a Series of date strings using pd.to_datetime(series, utc=True), what is the dtype of the resulting Series?
Astring
Bdatetime64[ns]
Cobject
Ddatetime64[ns, UTC]
Attempts:
2 left
💡 Hint
UTC conversion adds timezone info to dtype.

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

  1. Step 1: Understand the function purpose

    pd.to_datetime() is used to convert text or numbers that represent dates into pandas datetime objects.
  2. Step 2: Identify the correct description

    Among the options, only converting strings or numbers into datetime objects matches the function's purpose.
  3. Final Answer:

    Converts strings or numbers into datetime objects -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    df['date_str'] = pd.to_datetime(df['date_str']) -> Option A
  4. 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
3. What will be the output of this code?
import pandas as pd
dates = ['12/11/2023', '01/02/2024']
df = pd.DataFrame({'date': dates})
df['date'] = pd.to_datetime(df['date'], dayfirst=True)
print(df['date'])
medium
A. [Timestamp('2023-12-11'), Timestamp('2024-02-01')]
B. [Timestamp('2023-12-11'), Timestamp('2024-01-02')]
C. [Timestamp('2023-11-12'), Timestamp('2024-01-02')]
D. [Timestamp('2023-11-12'), Timestamp('2024-02-01')]

Solution

  1. Step 1: Understand dayfirst=True effect

    With dayfirst=True, the date strings are parsed as day/month/year.
  2. Step 2: Convert each date string

    '12/11/2023' becomes 12 Nov 2023 (2023-11-12), '01/02/2024' becomes 1 Feb 2024 (2024-02-01).
  3. Final Answer:

    [Timestamp('2023-11-12'), Timestamp('2024-02-01')] -> Option D
  4. Quick Check:

    dayfirst=True swaps day and month [OK]
Hint: dayfirst=True means day/month/year format [OK]
Common Mistakes:
  • Ignoring dayfirst and assuming month/day/year
  • Mixing up which part is day or month
  • Confusing output format with input string
4. Identify the error in this code snippet:
import pandas as pd
dates = ['2023-31-12', '2024-01-15']
df = pd.DataFrame({'date': dates})
df['date'] = pd.to_datetime(df['date'], format='%Y-%m-%d')
medium
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

  1. Step 1: Check input date format

    The dates are '2023-31-12' and '2024-01-15', which look like year-day-month format.
  2. Step 2: Compare with format string

    The format string '%Y-%m-%d' expects year-month-day, so it does not match the input dates.
  3. Final Answer:

    The date format string does not match the input dates -> Option A
  4. 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

  1. Step 1: Understand mixed date formats challenge

    Mixed formats cannot be parsed with a single fixed format string.
  2. 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.
  3. Step 3: Avoid rigid format or manual splitting

    Using a fixed format or manual splitting is inefficient and error-prone.
  4. Final Answer:

    Use pd.to_datetime(df['date_str'], errors='coerce') without format to parse mixed formats -> Option B
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard