0
0
Data Analysis Pythondata~20 mins

Date-based indexing and slicing in Data Analysis Python - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Date Slicing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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)
A
            value
2023-01-02     20
2023-01-03     30
2023-01-04     40
B
            value
2023-01-01     10
2023-01-02     20
2023-01-03     30
C
            value
2023-01-03     30
2023-01-04     40
2023-01-05     50
D
Empty DataFrame
Columns: [value]
Index: []
Attempts:
2 left
💡 Hint
Remember that slicing with dates in pandas includes both start and end dates.
data_output
intermediate
1: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))
A28
B31
C29
D30
Attempts:
2 left
💡 Hint
March always has 31 days.
🔧 Debug
advanced
1: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)
AKeyError
BIndexError
CValueError
DTypeError
Attempts:
2 left
💡 Hint
The date '2023-01-04' is not in the DataFrame index.
visualization
advanced
2: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()
AA bar plot with x-axis from 2023-01-01 to 2023-01-31
BAn empty plot with no data points
CA scatter plot with x-axis from 2023-05-01 to 2023-05-31
DA line plot with x-axis from 2023-04-01 to 2023-04-30 and y-axis from 90 to 119
Attempts:
2 left
💡 Hint
The slice '2023-04' selects all dates in April 2023.
🧠 Conceptual
expert
2:00remaining
Understanding partial string indexing behavior
Given a DataFrame indexed by daily dates, what happens when you slice with '2023-02'?
AIt raises a KeyError because '2023-02' is not a full date.
BIt returns only the row for February 1, 2023.
CIt returns all rows with dates in February 2023, including all days from 1 to 28 or 29.
DIt returns rows for February 2023 and March 2023.
Attempts:
2 left
💡 Hint
Pandas supports partial string indexing for date ranges.