An unreported charge has different consequences in a billing report and a data-quality report. Preserve the source amount long enough to decide how each report should handle it.

​
Find missing values

Use a null test to select transactions that need correction. An explicitly recorded zero is a valid observation.

from chalk import _, functions as F
from chalkdf import DataFrame

tx = DataFrame({
    "id": [1, 2, 3, 4], "amount": [12, None, 0, None],
    "estimate": [10, 8, 5, None],
})

tx.filter(F.is_null(_.amount)).select("id", "estimate").run()
#
idint64
estimateint64
128
24null

An estimate exists for ID 2. ID 4 has neither value.

​
Choose a fallback column

Use coalesce to take the first available value. Place the actual charge first and the estimate second.

tx.project({
    "id": _.id, "reported": _.amount,
    "best_available": F.coalesce(_.amount, _.estimate),
    "display_amount": F.coalesce(_.amount, _.estimate, 0),
}).run()
#
idint64
reportedint64
best_availableint64
display_amountint64
11121212
22null88
33000
44nullnull0

The recorded zero takes precedence over its estimate. A final literal zero gives ID 4 a display value while the best-available field still records the uncertainty.

​
Measure completeness before filling

Count rows and observed amounts separately. Compare an average over observed charges with one that assigns zero to every missing charge.

measured = tx.with_columns(
    F.is_not_null(_.amount).cast(int).alias("observed"),
    F.coalesce(_.amount, 0).alias("filled"),
)
measured.agg(
    [], _.count().alias("rows"), _.observed.sum().alias("reported"),
    _.amount.mean().alias("observed_mean"),
    _.filled.mean().alias("zero_filled_mean"),
).run()
#
rowsint64
reportedint64
observed_meandouble
zero_filled_meandouble
1426.03.0

Both averages are valid calculations. The observed mean answers how large reported charges were. The zero-filled mean spreads the reported total across every transaction.

​
Check floating-point NaN separately

A floating-point computation can produce NaN. Inspect the null indicator before using coalesce as a cleanup step.

samples = DataFrame({"value": [1.0, float("nan"), None]})
samples.project({
    "value": _.value, "missing": F.is_null(_.value),
    "filled": F.coalesce(_.value, 0.0),
}).run()
#
valuedouble
missingbool
filleddouble
11.0False1.0
2nanFalsenan
3nullTrue0.0

NaN survives this fill because it is a floating-point value. A source null takes the fallback.

​
Fill a gap from a neighboring reading

For regularly spaced samples, inspect both neighbors before estimating an isolated missing value. Order by time within each sensor so values cannot cross sensors.

readings = DataFrame({
    "sensor": ["a", "a", "a", "b"], "minute": [1, 2, 3, 1],
    "value": [10, None, 16, None],
})
neighbors = readings.with_columns(
    F.over(F.shift(_.value, 1), partition_by="sensor", order_by="minute").alias("previous"),
    F.over(F.shift(_.value, -1), partition_by="sensor", order_by="minute").alias("following"),
)
neighbors.project({
    "sensor": _.sensor, "minute": _.minute, "observed": _.value,
    "estimated": F.coalesce(_.value, (_.previous + _.following) / 2),
}).order_by("sensor", "minute").run()
#
sensorstring
minuteint64
observedint64
estimateddouble
1a11010.0
2a2null13.0
3a31616.0
4b1nullnull

Minute 2 is halfway between two readings and gets their midpoint. Sensor b has no neighbors, so its estimate stays null. For irregular intervals, weight the readings by elapsed time.

See casting and aggregations.