Bird
Raised Fist0
Pandasdata~20 mins

Time series analysis patterns in Pandas - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Time Series Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of resampling time series data

What is the output of this code that resamples daily data to monthly sums?

Pandas
import pandas as pd

dates = pd.date_range('2023-01-01', periods=6, freq='D')
data = pd.Series([1, 2, 3, 4, 5, 6], index=dates)
result = data.resample('M').sum()
print(result)
A
2023-01-01    21
Freq: M, dtype: int64
B
2023-01-31    6
Freq: M, dtype: int64
C
2023-01-31    21
Freq: M, dtype: int64
D
2023-01-06    21
Freq: D, dtype: int64
Attempts:
2 left
💡 Hint

Monthly resampling sums all daily values in January.

❓ data_output
intermediate
2:00remaining
Number of missing values after forward fill

Given this time series with missing values, how many missing values remain after forward filling?

Pandas
import pandas as pd
import numpy as np

dates = pd.date_range('2023-01-01', periods=5, freq='D')
data = pd.Series([np.nan, 2, np.nan, 4, np.nan], index=dates)
filled = data.ffill()
missing_count = filled.isna().sum()
print(missing_count)
A1
B0
C2
D3
Attempts:
2 left
💡 Hint

Forward fill replaces NaN with previous non-NaN value. The first NaN has no previous value.

❓ visualization
advanced
3:00remaining
Identifying seasonality in time series plot

Which plot best shows clear seasonality in monthly sales data?

Pandas
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np

np.random.seed(0)
dates = pd.date_range('2022-01-01', periods=24, freq='M')
sales = 10 + 5 * np.sin(2 * np.pi * dates.month / 12) + np.random.normal(0, 1, 24)
data = pd.Series(sales, index=dates)
plt.plot(data.index, data.values)
plt.title('Monthly Sales with Seasonality')
plt.xlabel('Date')
plt.ylabel('Sales')
plt.show()
AA histogram of sales values
BA scatter plot with random points and no pattern
CA bar chart with steadily increasing values
DA line plot showing repeating peaks every 12 months
Attempts:
2 left
💡 Hint

Seasonality means repeating patterns at regular intervals.

🧠 Conceptual
advanced
1:30remaining
Effect of differencing on time series data

What is the main effect of applying first-order differencing to a time series?

AIt increases the variance of the series
BIt removes trend and stabilizes the mean
CIt smooths the series by averaging values
DIt adds seasonality to the series
Attempts:
2 left
💡 Hint

Think about what subtracting consecutive values does to a trending series.

🔧 Debug
expert
2:00remaining
Error raised by incorrect time series indexing

What error does this code raise when trying to access a time series value?

Pandas
import pandas as pd

dates = pd.date_range('2023-01-01', periods=3, freq='D')
data = pd.Series([10, 20, 30], index=dates)
value = data['2023-01-04']
print(value)
AKeyError
BIndexError
CTypeError
DValueError
Attempts:
2 left
💡 Hint

Check if the date key exists in the index.

Practice

(1/5)
1. What is the main purpose of using a rolling average in time series analysis with pandas?
easy
A. To convert time series data into categorical data
B. To remove all missing values from the data
C. To smooth out short-term fluctuations and highlight longer-term trends
D. To sort the data by date in ascending order

Solution

  1. Step 1: Understand rolling average concept

    A rolling average calculates the mean of data points in a moving window, smoothing short-term noise.
  2. Step 2: Identify its effect on time series data

    This smoothing helps reveal longer-term trends by reducing random fluctuations.
  3. Final Answer:

    To smooth out short-term fluctuations and highlight longer-term trends -> Option C
  4. Quick Check:

    Rolling average = smoothing trends [OK]
Hint: Rolling average smooths data to show trends clearly [OK]
Common Mistakes:
  • Thinking rolling average removes missing values
  • Confusing rolling average with sorting
  • Assuming it changes data type
2. Which of the following is the correct pandas syntax to calculate a 7-day rolling mean on a DataFrame column df['sales']?
easy
A. df['sales'].rolling(window=7).mean()
B. df['sales'].rolling(7).sum()
C. df.rolling(window=7)['sales'].sum()
D. df['sales'].rolling(window=7).median()

Solution

  1. Step 1: Recall rolling syntax in pandas

    The rolling method is called on the Series with window=7 to specify 7 days.
  2. Step 2: Apply mean function to get rolling average

    Using .mean() after rolling computes the rolling mean correctly.
  3. Final Answer:

    df['sales'].rolling(window=7).mean() -> Option A
  4. Quick Check:

    rolling(window=7).mean() = correct syntax [OK]
Hint: Use rolling(window=7).mean() for 7-day rolling average [OK]
Common Mistakes:
  • Using rolling(7) without window= keyword
  • Using sum() instead of mean() for average
  • Applying rolling on DataFrame instead of Series
3. Given the following code, what will be the output of print(diff_series)?
import pandas as pd
s = pd.Series([10, 15, 20, 18, 25])
diff_series = s.diff()
medium
A. [NaN, 5.0, 5.0, -2.0, 7.0]
B. [10, 15, 20, 18, 25]
C. [NaN, 10, 15, 20, 18]
D. [5, 5, -2, 7, NaN]

Solution

  1. Step 1: Understand what diff() does on a Series

    The diff() method calculates the difference between each element and its previous element.
  2. Step 2: Calculate differences step-by-step

    Differences: NaN (no previous), 15-10=5, 20-15=5, 18-20=-2, 25-18=7.
  3. Final Answer:

    [NaN, 5.0, 5.0, -2.0, 7.0] -> Option A
  4. Quick Check:

    diff() = current - previous [OK]
Hint: diff() shows change from previous value [OK]
Common Mistakes:
  • Forgetting first value is NaN
  • Confusing diff() with cumulative sum
  • Misordering differences
4. The following code throws an error. What is the cause?
import pandas as pd
dates = pd.date_range('2023-01-01', periods=5)
df = pd.DataFrame({'value': [1, 2, 3, 4, 5]}, index=dates)
rolling_sum = df.rolling(3).sum()
print(rolling_sum['value'])
medium
A. The rolling window size must be specified with window=3
B. There is no error; the code runs correctly
C. The rolling method cannot be used on DataFrames
D. The DataFrame index must be reset before rolling

Solution

  1. Step 1: Check rolling syntax on DataFrame

    Using df.rolling(3).sum() is valid; window size can be passed as positional argument.
  2. Step 2: Verify if code runs without error

    Rolling sum on DataFrame with datetime index works fine; no reset needed.
  3. Final Answer:

    There is no error; the code runs correctly -> Option B
  4. Quick Check:

    rolling(3) on DataFrame = valid [OK]
Hint: rolling() accepts window as positional argument [OK]
Common Mistakes:
  • Assuming window= is mandatory
  • Thinking index must be reset
  • Believing rolling only works on Series
5. You have daily sales data in a DataFrame df with a datetime index. You want to detect if sales on a day are unusually high compared to the previous week. Which pandas method combination is best to identify these spikes?
hard
A. Sort the DataFrame by sales and pick top 7 days
B. Use diff() to find daily changes and select days with positive differences
C. Apply autocorrelation to find repeated patterns
D. Calculate 7-day rolling mean and then find days where sales exceed mean by a threshold

Solution

  1. Step 1: Understand the goal to find spikes compared to previous week

    We want to compare each day's sales to the average of the last 7 days.
  2. Step 2: Use rolling mean to get weekly average and compare

    Calculating 7-day rolling mean smooths data; days exceeding this mean by threshold indicate spikes.
  3. Final Answer:

    Calculate 7-day rolling mean and then find days where sales exceed mean by a threshold -> Option D
  4. Quick Check:

    Rolling mean + threshold = spike detection [OK]
Hint: Compare daily sales to 7-day rolling mean for spikes [OK]
Common Mistakes:
  • Using diff() only finds changes, not spikes
  • Sorting ignores time context
  • Autocorrelation finds patterns, not spikes
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