Challenge - 5 Problems
Date Slicing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of date-based slicing on a DataFrame
What is the output of this code snippet that slices a DataFrame by date?
Data Analysis Python
import pandas as pd dates = pd.date_range('2023-01-01', periods=5) data = pd.DataFrame({'value': [10, 20, 30, 40, 50]}, index=dates) result = data['2023-01-02':'2023-01-04'] print(result)
Attempts:
2 left
💡 Hint
Remember that slicing with dates in pandas includes both start and end dates.
✗ Incorrect
When slicing a DataFrame by date strings, pandas includes both the start and end dates in the slice. Here, the slice from '2023-01-02' to '2023-01-04' includes those three dates and their corresponding values.
❓ data_output
intermediate1:30remaining
Number of rows after date-based filtering
How many rows remain after filtering this DataFrame for dates in March 2023?
Data Analysis Python
import pandas as pd import numpy as np np.random.seed(0) dates = pd.date_range('2023-01-01', '2023-06-30') data = pd.DataFrame({'value': np.random.randint(1, 100, len(dates))}, index=dates) filtered = data['2023-03'] print(len(filtered))
Attempts:
2 left
💡 Hint
March always has 31 days.
✗ Incorrect
The slice '2023-03' selects all rows with dates in March 2023. March has 31 days, so the filtered DataFrame has 31 rows.
🔧 Debug
advanced1:30remaining
Identify the error in date-based slicing code
What error does this code raise when trying to slice a DataFrame by date?
Data Analysis Python
import pandas as pd dates = pd.date_range('2023-01-01', periods=3) data = pd.DataFrame({'value': [1, 2, 3]}, index=dates) result = data['2023-01-04'] print(result)
Attempts:
2 left
💡 Hint
The date '2023-01-04' is not in the DataFrame index.
✗ Incorrect
Trying to access a single date not present in the index raises a KeyError because pandas cannot find that label.
❓ visualization
advanced2:30remaining
Plotting data sliced by date range
Which option shows the correct plot output after slicing the DataFrame for April 2023?
Data Analysis Python
import pandas as pd import matplotlib.pyplot as plt dates = pd.date_range('2023-01-01', '2023-06-30') values = range(len(dates)) data = pd.DataFrame({'value': values}, index=dates) slice_april = data['2023-04'] slice_april.plot() plt.show()
Attempts:
2 left
💡 Hint
The slice '2023-04' selects all dates in April 2023.
✗ Incorrect
The slice selects dates from April 1 to April 30, which correspond to indices 90 to 119 in the original range. The plot is a line plot by default showing these values.
🧠 Conceptual
expert2:00remaining
Understanding partial string indexing behavior
Given a DataFrame indexed by daily dates, what happens when you slice with '2023-02'?
Attempts:
2 left
💡 Hint
Pandas supports partial string indexing for date ranges.
✗ Incorrect
When slicing with a partial date string like '2023-02', pandas returns all rows with dates starting with that string, which means all days in February 2023.