Write a pandas function that forward-fills missing sensor readings separately for each sensor, leaves leading gaps empty, and returns a new DataFrame with the original index and row order. It tests group-wise fills, index alignment and how fill logic depends on time order.
Given a DataFrame with `'sensor'` (group key) and numeric `'value'` (with `NaN`s), already time-ordered within each sensor, implement `ffill_within_group` to forward-fill `'value'` independently per sensor.
The function must meet these requirements:
- Never fill a gap from another sensor's reading.
- Leading `NaN`s in a group with no prior value stay `NaN`.
- Preserve shape, columns, row order, and index.
- Return a new DataFrame: do not mutate `df`.
Example:
```python
# input
sensor value
0 A 1.0
1 A NaN
2 B NaN
3 B 5.0
4 A NaN
5 B NaN
# output
sensor value
0 A 1.0
1 A 1.0
2 B NaN # leading NaN in B stays NaN
3 B 5.0
4 A 1.0
5 B 5.0
```
```hint Scope the fill
A plain `ffill()` on the column only looks at the row directly above. Find a way to run the fill on each sensor's rows separately while the result stays aligned to the original index.
```
### What a Strong Answer Covers
- Scoping the fill to each sensor, so row 4 takes A's last reading and row 2 is left empty even though an A reading sits above it.
- Why leading `NaN`s stay `NaN` and why the existing time order is what makes a per-group fill correct.
- Returning a new DataFrame with the same index, row order and columns, without touching the input.
- Edge cases such as a sensor with no readings at all or rows whose `sensor` value is missing.
### Follow-up Questions
- How would you also cap the fill so a value only carries forward for a limited number of rows?
- What if rows were not pre-sorted by time within each sensor?
- How would you cap the fill by elapsed time, for example only carry a reading forward for 10 minutes?
Overview: Write a pandas function that forward-fills missing sensor readings separately for each sensor, leaves leading gaps empty, and returns a new DataFrame with the original index and row order. It tests group-wise fills, index alignment and how fill logic depends on time order.
Given a DataFrame with 'sensor' (group key) and numeric 'value' (with NaNs), already time-ordered within each sensor, implement ffill_within_group to forward-fill 'value' independently per sensor.
The function must meet these requirements:
Never fill a gap from another sensor's reading.
Leading
NaN
s in a group with no prior value stay
NaN
.
Preserve shape, columns, row order, and index.
Return a new DataFrame: do not mutate
df
.
Example:
# input
sensor value
0 A 1.0
1 A NaN
2 B NaN
3 B 5.0
4 A NaN
5 B NaN
# output
sensor value
0 A 1.0
1 A 1.0
2 B NaN # leading NaN in B stays NaN
3 B 5.0
4 A 1.0
5 B 5.0
What a Strong Answer Covers Guidance
Scoping the fill to each sensor, so row 4 takes A's last reading and row 2 is left empty even though an A reading sits above it.
Why leading
NaN
s stay
NaN
and why the existing time order is what makes a per-group fill correct.
Returning a new DataFrame with the same index, row order and columns, without touching the input.
Edge cases such as a sensor with no readings at all or rows whose
sensor
value is missing.
Follow-up Questions Guidance
How would you also cap the fill so a value only carries forward for a limited number of rows?
What if rows were not pre-sorted by time within each sensor?
How would you cap the fill by elapsed time, for example only carry a reading forward for 10 minutes?