0
0
Pandasdata~20 mins

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

Choose your learning style9 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.