Bird
Raised Fist0
Pandasdata~20 mins

Date range creation with date_range in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Date Range Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of daily date range creation
What is the output of the following code snippet that creates a daily date range?
Pandas
import pandas as pd

dates = pd.date_range(start='2024-01-01', end='2024-01-05')
print(dates)
ADatetimeIndex(['2024-01-01', '2024-01-03', '2024-01-05'], dtype='datetime64[ns]', freq='2D')
BDatetimeIndex(['2024-01-01', '2024-01-02', '2024-01-03', '2024-01-05'], dtype='datetime64[ns]', freq='D')
CDatetimeIndex(['2024-01-01', '2024-01-02', '2024-01-04', '2024-01-05'], dtype='datetime64[ns]', freq='D')
DDatetimeIndex(['2024-01-01', '2024-01-02', '2024-01-03', '2024-01-04', '2024-01-05'], dtype='datetime64[ns]', freq='D')
Attempts:
2 left
💡 Hint
Check how pandas.date_range includes both start and end dates by default with daily frequency.
❓ data_output
intermediate
1:30remaining
Number of periods in a weekly date range
How many dates are generated by this weekly date range starting on 2024-01-01 for 4 periods?
Pandas
import pandas as pd

dates = pd.date_range(start='2024-01-01', periods=4, freq='W')
print(len(dates))
A1
B4
C5
D3
Attempts:
2 left
💡 Hint
The periods parameter controls how many dates are generated.
🔧 Debug
advanced
2:00remaining
Identify the error in this date_range code
What error does this code produce and why? import pandas as pd dates = pd.date_range(start='2024-01-01', end='2024-01-10', freq='2X') print(dates)
Pandas
import pandas as pd

dates = pd.date_range(start='2024-01-01', end='2024-01-10', freq='2X')
print(dates)
AValueError: Invalid frequency: 2X
BTypeError: freq must be a string
CSyntaxError: invalid syntax
DNo error, prints dates every 2 days
Attempts:
2 left
💡 Hint
Check if '2X' is a valid frequency alias in pandas.
🚀 Application
advanced
2:00remaining
Create a date range for business days excluding weekends
Which code snippet correctly creates a date range of 5 business days starting from 2024-01-01?
Apd.date_range(start='2024-01-01', periods=5, freq='B')
Bpd.date_range(start='2024-01-01', end='2024-01-05', freq='B')
Cpd.date_range(start='2024-01-01', periods=5, freq='D')
Dpd.date_range(start='2024-01-01', end='2024-01-10', freq='W')
Attempts:
2 left
💡 Hint
Business day frequency is represented by 'B' in pandas.
🧠 Conceptual
expert
2:30remaining
Understanding the effect of 'closed' parameter in date_range
Given the code below, which dates are included in the resulting date range? import pandas as pd dates = pd.date_range(start='2024-01-01', end='2024-01-05', closed='left') print(dates)
Pandas
import pandas as pd

dates = pd.date_range(start='2024-01-01', end='2024-01-05', closed='left')
print(dates)
ADatetimeIndex(['2024-01-01', '2024-01-05'], dtype='datetime64[ns]', freq='D')
BDatetimeIndex(['2024-01-02', '2024-01-03', '2024-01-04', '2024-01-05'], dtype='datetime64[ns]', freq='D')
CDatetimeIndex(['2024-01-01', '2024-01-02', '2024-01-03', '2024-01-04'], dtype='datetime64[ns]', freq='D')
DDatetimeIndex(['2024-01-02', '2024-01-03', '2024-01-04'], dtype='datetime64[ns]', freq='D')
Attempts:
2 left
💡 Hint
The 'closed' parameter controls which endpoint is excluded from the range.

Practice

(1/5)
1. What does the pandas.date_range function primarily do?
easy
A. Converts strings to datetime objects
B. Calculates the difference between two dates
C. Creates a sequence of dates between specified start and end points
D. Sorts dates in ascending order

Solution

  1. Step 1: Understand the purpose of date_range

    date_range generates a list of dates based on start, end, or periods parameters.
  2. Step 2: Compare with other options

    Options B, C, and D describe different date operations, not date sequence creation.
  3. Final Answer:

    Creates a sequence of dates between specified start and end points -> Option C
  4. Quick Check:

    date_range = sequence of dates [OK]
Hint: Remember: date_range makes date lists, not calculations [OK]
Common Mistakes:
  • Confusing date_range with date difference functions
  • Thinking it converts strings to dates
  • Assuming it sorts dates instead of creating them
2. Which of the following is the correct syntax to create a date range from January 1, 2023 to January 5, 2023 using pandas?
easy
A. pd.date_range('2023-01-01', start='2023-01-05')
B. pd.date_range(start='2023-01-01', end='2023-01-05')
C. pd.date_range('2023-01-01', periods=3)
D. pd.date_range(end='2023-01-01', start='2023-01-05')

Solution

  1. Step 1: Check parameter names and order

    The correct syntax uses named parameters start and end with date strings.
  2. Step 2: Validate each option

    A: pd.date_range('2023-01-01', start='2023-01-05') mixes positional start and named start, causing TypeError (multiple values for start).
    B: pd.date_range(start='2023-01-01', end='2023-01-05') correctly uses named start and end to create dates Jan 1-5.
    C: pd.date_range('2023-01-01', periods=3) creates only 3 dates (Jan 1-3), not up to Jan 5.
    D: pd.date_range(end='2023-01-01', start='2023-01-05') reverses start and end (start > end), producing empty range.
  3. Final Answer:

    pd.date_range(start='2023-01-01', end='2023-01-05') -> Option B
  4. Quick Check:

    Use start= and end= for clarity [OK]
Hint: Use named parameters start= and end= for clear date ranges [OK]
Common Mistakes:
  • Swapping start and end parameters
  • Omitting parameter names causing confusion
  • Using periods without specifying start or end
3. What will be the output of the following code?
import pandas as pd
rng = pd.date_range(start='2023-01-01', periods=3, freq='D')
print(list(rng))
medium
A. Error: freq parameter not recognized
B. [Timestamp('2023-01-01 00:00:00'), Timestamp('2023-01-01 00:00:00'), Timestamp('2023-01-01 00:00:00')]
C. [Timestamp('2023-01-01 00:00:00'), Timestamp('2023-01-03 00:00:00'), Timestamp('2023-01-05 00:00:00')]
D. [Timestamp('2023-01-01 00:00:00'), Timestamp('2023-01-02 00:00:00'), Timestamp('2023-01-03 00:00:00')]

Solution

  1. Step 1: Understand parameters used

    start='2023-01-01', periods=3 means 3 dates starting from Jan 1, 2023. freq='D' means daily frequency.
  2. Step 2: Generate dates based on parameters

    Dates will be Jan 1, Jan 2, and Jan 3, each at midnight (00:00:00).
  3. Final Answer:

    [Timestamp('2023-01-01 00:00:00'), Timestamp('2023-01-02 00:00:00'), Timestamp('2023-01-03 00:00:00')] -> Option D
  4. Quick Check:

    periods=3 with daily freq = 3 consecutive days [OK]
Hint: periods + freq='D' creates consecutive daily dates [OK]
Common Mistakes:
  • Assuming freq='D' means hourly or monthly
  • Confusing periods with end date
  • Expecting repeated same date
4. The following code throws an error. What is the mistake?
import pandas as pd
rng = pd.date_range(start='2023-01-01', end='2023-01-05', periods=3)
print(rng)
medium
A. The freq parameter is missing
B. The start date format is incorrect
C. The code is correct and should not throw an error
D. You cannot use both end and periods together

Solution

  1. Step 1: Analyze parameters used

    The code uses start, end, and periods together but omits freq.
  2. Step 2: Understand pandas.date_range rules

    When both start, end, and periods are given, freq must be specified to determine spacing. Without freq, pandas throws an error.
  3. Final Answer:

    The freq parameter is missing -> Option A
  4. Quick Check:

    freq needed when start, end, and periods all set [OK]
Hint: Specify freq when using start, end, and periods together [OK]
Common Mistakes:
  • Thinking periods and end cannot be combined
  • Assuming date format is wrong
  • Believing code runs without freq
5. You want to create a date range for every Monday in March 2023. Which code snippet correctly achieves this?
hard
A. pd.date_range(start='2023-03-06', end='2023-03-27', freq='W-MON')
B. pd.date_range(start='2023-03-01', periods=4, freq='W')
C. pd.date_range(start='2023-03-01', end='2023-03-31', freq='W')
D. pd.date_range(start='2023-03-01', end='2023-03-31', freq='M')

Solution

  1. Step 1: Identify Mondays in March 2023

    Mondays in March 2023 are 6th, 13th, 20th, and 27th.
  2. Step 2: Check each option's frequency and date range

    A: pd.date_range(start='2023-03-01', end='2023-03-31', freq='W') uses freq='W' (defaults to W-SUN), producing Sundays Mar 5,12,19,26.
    B: pd.date_range(start='2023-03-01', periods=4, freq='W') uses Sunday weeks with fixed 4 periods, not Mondays.
    C: pd.date_range(start='2023-03-06', end='2023-03-27', freq='W-MON') starts on first Monday (Mar 6), ends on last (Mar 27), correctly lists all 4 Mondays.
    D: pd.date_range(start='2023-03-01', end='2023-03-31', freq='M') uses month-end frequency, not weekly Mondays.
  3. Final Answer:

    pd.date_range(start='2023-03-06', end='2023-03-27', freq='W-MON') -> Option A
  4. Quick Check:

    freq='W-MON' with correct start/end picks Mondays [OK]
Hint: Use freq='W-MON' and start on first Monday [OK]
Common Mistakes:
  • Using freq='W' which defaults to Sunday
  • Starting date not aligned with Monday
  • Using freq='M' which selects month ends
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