Data Analyst Interview
Menu
Browse in your language. All mock interviews, preparation sessions and feedback are in English only.

Can pandas ffill Copy a Value Between Accounts?

Python · Missing-value checks

Can pandas ffill Copy a Value Between Accounts?

Test entity boundaries, chronological order, and the meaning of missing updates.

By Prakhar Shrivastava · October 20, 2026

Yes. Applying ffill() to a whole column can carry a value from one account into another account’s row. If the source contract permits carrying values forward, sort the observations chronologically and fill within the entity key. Grouping prevents cross-account propagation; it does not prove that filling is justified.

Reproduce a cross-account value

This fictional export contains spending limits stated in USD for US, UK, and German accounts. A missing value means “no update in this row” only for the purpose of this exercise. We assume a previously known limit remains valid until an explicit update. This assumption would be inappropriate if missing meant revoked, unknown, or unavailable.

import pandas as pd
x = pd.DataFrame({
    'account': ['US-A','UK-B','US-A','UK-B','DE-C'],
    'day': pd.to_datetime(['2026-10-01','2026-10-01',
                           '2026-10-02','2026-10-02','2026-10-02']),
    'limit_usd': [100, None, None, 80, None]
})
x = x.sort_values(['day','account']).reset_index(drop=True)
x['unsafe'] = x['limit_usd'].ffill()
x['within_account'] = x.groupby('account')['limit_usd'].ffill()
print(x[['account','day','limit_usd','unsafe','within_account']])

In pandas 2.2.3, the German account’s October 2 row receives $100 in unsafe, copied from an earlier US row. Yet that German account has no recorded limit. The within_account result remains missing for Germany. The US October 2 row receives its own previous $100, while the UK October 2 row retains its observed $80. The UK October 1 row remains missing because there is no earlier UK value.

Decide what a missing observation means

Carry-forward is a data interpretation, not simply a way to make a chart look complete. For a state that persists until changed, a missing update may reasonably inherit a prior state. For a daily measurement, a missing value may mean the sensor failed or the source did not deliver a reading. Filling those measurements silently invents observations.

Keep the raw column and the derived column. Add a separate indicator for values that were carried, so reviewers can distinguish observed values from assumptions. For this fixture, the mask limit_usd.isna() & within_account.notna() identifies the carried US October 2 value. Do not describe it as a fresh source measurement.

Order records before filling

Forward fill follows row order. A dataset sorted newest-first could carry a later state into an earlier date. Sort ascending by the relevant time field and resolve conflicting records with the same entity and timestamp before filling. This example has no timestamp ties within an account. An arbitrary row number is not a valid conflict-resolution rule unless the source explicitly assigns it that meaning.

When restoring the original display order, retain a stable row identifier. Avoid assigning a separately reordered array by position; the Series assignment example explains how row identity and index alignment can diverge.

A row limit is not an expiration time

The limit argument constrains how many consecutive missing observations are filled. It does not say that a value is no more than a day or an hour old. Two adjacent observations might be a minute apart or a month apart. If the business rule includes expiration, carry or join the source timestamp too and compare the actual age with the allowed interval. The time-aware reference lookup demonstrates an explicit age tolerance.

Missing account keys need their own exception path. Do not put unrelated unidentified accounts into one synthetic “unknown” entity and propagate a value across them. Grouped filling can only respect the identity information it receives. Likewise, a newly observed zero should remain zero if it is valid; it is not a missing value to replace.

Verification and further reading

Five assertions passed in pandas 2.2.3: the observed UK $80 is retained, the US carries its own $100, Germany remains missing after grouped filling, the ungrouped fill incorrectly assigns Germany $100, and the first UK observation remains missing. Test these boundary cases with your actual data contract before extending the pattern.

Primary references: pandas 2.2 grouped forward-fill documentation and the current GroupBy reference. For a separate missing-value reporting issue, see why a known subtotal may still be incomplete.

Leave a Comment

Discover more from Data Analyst Interview

Subscribe now to keep reading and get access to the full archive.

Continue reading

✉ WhatsApp