Given a time series data, what is the output of the rolling mean calculation shown below?
import pandas as pd data = pd.Series([10, 20, 30, 40, 50], index=pd.date_range('2024-01-01', periods=5)) rolling_mean = data.rolling(window=3).mean() print(rolling_mean.tolist())
Remember, rolling mean with window=3 needs 3 values to compute the first mean.
The rolling mean with window size 3 returns NaN for the first two values because there are not enough data points to calculate the mean. Starting from the third value, it calculates the mean of the current and previous two values.
Using the following time series data, how many peaks are detected by the code?
import numpy as np from scipy.signal import find_peaks data = np.array([1, 3, 7, 1, 2, 6, 0, 1]) peaks, _ = find_peaks(data) print(len(peaks))
Peaks are points higher than their neighbors.
The peaks are at indices 2 (value 7), 5 (value 6), and 7 (value 1, which is higher than its left neighbor 0). Total of 3 peaks.
Given these four plots of time series data, which one clearly shows an upward trend?
import matplotlib.pyplot as plt import pandas as pd import numpy as np dates = pd.date_range('2024-01-01', periods=50) data1 = pd.Series(np.random.normal(0, 1, 50), index=dates) data2 = pd.Series(np.linspace(0, 10, 50) + np.random.normal(0, 1, 50), index=dates) data3 = pd.Series(np.sin(np.linspace(0, 10, 50)), index=dates) data4 = pd.Series(np.random.normal(5, 1, 50), index=dates) plt.figure(figsize=(10, 6)) plt.subplot(2, 2, 1) plt.plot(data1) plt.title('A') plt.subplot(2, 2, 2) plt.plot(data2) plt.title('B') plt.subplot(2, 2, 3) plt.plot(data3) plt.title('C') plt.subplot(2, 2, 4) plt.plot(data4) plt.title('D') plt.tight_layout() plt.show()
Look for a steady increase in values over time.
Plot B shows a line with a positive slope plus some noise, indicating an upward trend. Others show noise, oscillation, or no clear trend.
What error will this code raise when trying to resample a time series?
import pandas as pd data = pd.Series([1, 2, 3], index=[1, 2, 3]) resampled = data.resample('D').mean() print(resampled)
Check the index type required for resampling.
Resampling requires a time-based index like DatetimeIndex. Here the index is integers, so it raises a TypeError.
You have monthly sales data for 5 years. Which method best reveals seasonal trends?
Seasonal trends repeat every year, so smoothing over 12 months helps.
A 12-month rolling average smooths out short-term fluctuations and highlights yearly seasonal patterns. Linear regression fits a trend but misses seasonality. Histogram shows distribution, not time trends. Daily differences are irrelevant for monthly data.