What is the output of this code that resamples daily data to monthly sums?
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)
Monthly resampling sums all daily values in January.
The code sums all values from Jan 1 to Jan 6. Since the frequency is monthly ('M'), the result is indexed by the last day of January with the sum 1+2+3+4+5+6=21.
Given this time series with missing values, how many missing values remain after forward filling?
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)
Forward fill replaces NaN with previous non-NaN value. The first NaN has no previous value.
The first value is NaN and has no previous value to fill from, so it remains NaN. The other NaNs get filled. So 1 missing value remains.
Which plot best shows clear seasonality in monthly sales data?
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()
Seasonality means repeating patterns at regular intervals.
The line plot shows a smooth wave pattern repeating every 12 months, indicating seasonality.
What is the main effect of applying first-order differencing to a time series?
Think about what subtracting consecutive values does to a trending series.
First-order differencing subtracts each value from the previous one, removing linear trends and making the mean more stable.
What error does this code raise when trying to access a time series value?
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)
Check if the date key exists in the index.
The date '2023-01-04' is not in the index, so accessing it raises a KeyError.