Forward Then Backward Fill

Quick Overview

Write a pandas function that fills missing values in an ordered readings column by filling forward and then backward, returning a new DataFrame with the same shape and index. It tests the order of fill operations, leading gaps, all-missing columns and group-wise filling.

Forward Then Backward Fill

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Implement `fill_forward_then_backward(df)` for a DataFrame with a numeric column `'reading'` already in order. Forward fill first, then backward fill any remaining leading `NaN`s (equivalent to `df.ffill().bfill()`). The function must meet these requirements: - Return a new DataFrame, leaving `df` unchanged. - Preserve shape, columns, and index. - Do not sort. Example: ```python df['reading'] = [NaN, 2.0, NaN, NaN, 5.0, NaN] # ffill: [NaN, 2.0, 2.0, 2.0, 5.0, 5.0] # then bfill leading NaN: [2.0, 2.0, 2.0, 2.0, 5.0, 5.0] ``` ```hint Which gaps survive After the forward pass, look at where the remaining `NaN`s can be. Ask which direction of fill reaches exactly those positions and nothing else. ``` ### What a Strong Answer Covers - The order of the two passes, and what the example would give if the backward fill ran first. - Why the backward pass only changes leading `NaN`s once the forward pass has run. - Returning a new DataFrame with the same shape, columns and index, without sorting or mutating the input. - Columns that are entirely `NaN`, and whether columns other than `'reading'` should be filled too. ### Follow-up Questions - What if an entire column is `NaN`? - How would you fill within groups instead of the whole frame? - Why can the backward fill be a problem when the filled data feeds a forecasting model?

Overview: Write a pandas function that fills missing values in an ordered readings column by filling forward and then backward, returning a new DataFrame with the same shape and index. It tests the order of fill operations, leading gaps, all-missing columns and group-wise filling.

Nov 25, 2026
easyData ScientistOtherData Manipulation (SQL/Python)
0
0

Implement fill_forward_then_backward(df) for a DataFrame with a numeric column 'reading' already in order. Forward fill first, then backward fill any remaining leading NaNs (equivalent to df.ffill().bfill()).

The function must meet these requirements:

  • Return a new DataFrame, leaving df unchanged.
  • Preserve shape, columns, and index.
  • Do not sort.

Example:

df['reading'] = [NaN, 2.0, NaN, NaN, 5.0, NaN]
# ffill: [NaN, 2.0, 2.0, 2.0, 5.0, 5.0]
# then bfill leading NaN: [2.0, 2.0, 2.0, 2.0, 5.0, 5.0]

What a Strong Answer Covers Guidance

  • The order of the two passes, and what the example would give if the backward fill ran first.
  • Why the backward pass only changes leading NaN s once the forward pass has run.
  • Returning a new DataFrame with the same shape, columns and index, without sorting or mutating the input.
  • Columns that are entirely NaN , and whether columns other than 'reading' should be filled too.

Follow-up Questions Guidance

  • What if an entire column is NaN ?
  • How would you fill within groups instead of the whole frame?
  • Why can the backward fill be a problem when the filled data feeds a forecasting model?