Sample questions
Fe Temporal Cyclical FeaturesDifficulty 1
In pandas 2.3 a Series s holds [10, 20, 30, 40, 50] on the integer index 0..4. A lag feature is built with s.shift(1). What does the result contain?
- a[20, 30, 40, 50, NaN], because each value is pulled one position earlier.
- b[10, 20, 30, 40, 50], because a lag relabels the index and leaves the values in place.
- c[NaN, 10, 20, 30, 40], because
shift moves each value one position later.✓ - d[NaN, NaN, 10, 20, 30], because a one-step lag empties two rows at the head.
Explanation:A positive shift argument pushes values downward along the index, so position i receives the value that used to sit at position i-1. The first position has no predecessor and becomes NaN, and the last original value (50) falls off the end. Exactly one NaN appears for shift(1).
Fe Temporal Cyclical FeaturesDifficulty 2
A junior engineer writes df['next_day_price'] = df['price'].shift(-1) on a table sorted by date, one row per day. Mechanically, what value does row t receive?
- aThe price recorded on day t+1, one calendar step ahead of row t.✓
- bThe price on day t-1, since a negative sign reverses the sort order first.
- cThe price on day t itself, because pandas clips negative arguments to zero.
- dThe mean of all prices after day t, since a negative shift aggregates forward.
Explanation:A negative shift argument pulls values upward along the index: position t takes the value from position t+1. On a date-sorted daily table that is literally tomorrow's price sitting in today's row, and the final row becomes NaN because it has no successor. No aggregation or reordering happens.
Fe Temporal Cyclical FeaturesDifficulty 2
A daily table has 100 rows, one per day, with no gaps. The team adds seven lag columns lag_1 through lag_7 built with shift(1) .. shift(7), then calls dropna(). How many rows survive, and why?
- a100 rows, because
dropna() only removes rows where every column is missing. - b99 rows, because only the very first row can be missing a predecessor.
- c86 rows, because each of the seven lags independently costs two rows at the head.
- d93 rows, because
lag_7 is undefined for the first seven positions and that column dominates.✓
Explanation:shift(k) leaves exactly k NaNs at the head, so lag_1 has 1, lag_2 has 2, and lag_7 has 7. Any row missing at least one column is dropped, so the head loss is the maximum over the columns, not the sum: rows 0..6 go, leaving 93. Measured on pandas 2.3 the first surviving index is 7.
Fe Temporal Cyclical FeaturesDifficulty 1
With pandas 2.3, pd.Series([2, 4, 6, 8, 10]).rolling(3).mean() is evaluated. What is printed?
- a[2.0, 3.0, 4.0, 6.0, 8.0]
- b[NaN, NaN, 4.0, 6.0, 8.0]✓
- c[NaN, NaN, NaN, 4.0, 6.0]
- d[NaN, 3.0, 4.0, 6.0, 8.0, NaN]
Explanation:For an integer window, min_periods defaults to the window size, so the first two positions have fewer than three observations and stay NaN. From position 2 onward each output is the mean of that position and the two before it: (2+4+6)/3 = 4, (4+6+8)/3 = 6, (6+8+10)/3 = 8. The output length always equals the input length.
Fe Temporal Cyclical FeaturesDifficulty 3
To inspect what a window actually contains, an engineer runs on pandas 2.3: pd.Series([1., 2., 3., 4., 5.]).rolling(3).apply(lambda w: w[-1], raw=True). The result is [NaN, NaN, 3.0, 4.0, 5.0]. What does this output establish about rolling(3)?
- aThe window is anchored on the previous row, so the label row stays outside its own window.
- b
apply receives the window reversed, so w[-1] is really the oldest observation in it. - cThe row carrying the label is the last element of its own window.✓
- dThe window is centred on the label row, so
w[-1] is one row ahead of it.
Explanation:w[-1] returns the final element of each window, and it reproduces the original value at every labelled position. That can only happen if the labelled row sits at the right edge of its window, so rolling(3) at position i covers positions i-2, i-1 and i. Running the same call with w[0] returns [NaN, NaN, 1.0, 2.0, 3.0], the value from two rows back, which confirms the same span.
Fe Temporal Cyclical FeaturesDifficulty 3
On a Series s = [4, 8, 12, 16, 20] in pandas 2.3, two candidate features are compared row by row: A = s.rolling(3).mean() gives [NaN, NaN, 8, 12, 16] and B = s.shift(1).rolling(3).mean() gives [NaN, NaN, NaN, 8, 12]. Which statement describes the mechanical relationship between them?
- aB is A moved down one row, so B at position i averages positions i-3..i-1 while A includes position i.✓
- bB uses a window of four observations and only becomes defined one row later than A.
- cB averages the same three positions as A but divides by the shifted observation count, so the values differ.
- dB is centred while A is trailing, so the two differ at both ends of this 5-row Series.
Explanation:The shift(1) moves every observation one position later before the window is applied, so the window that lands on position i sees positions i-3, i-2 and i-1. A's window on the same position covers i-2, i-1 and i. Measured on this Series, B's value stream is A's value stream delayed by exactly one row, and the divisor is three in both.