Chalk DataFrames
Distinguish missing, empty, and zero values and measure the effect of a fallback.
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.
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 |
|---|---|---|
| 1 | 2 | 8 |
| 2 | 4 | null |
An estimate exists for ID 2. ID 4 has neither value.
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 |
|---|---|---|---|---|
| 1 | 1 | 12 | 12 | 12 |
| 2 | 2 | null | 8 | 8 |
| 3 | 3 | 0 | 0 | 0 |
| 4 | 4 | null | null | 0 |
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.
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 |
|---|---|---|---|---|
| 1 | 4 | 2 | 6.0 | 3.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.
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 |
|---|---|---|---|
| 1 | 1.0 | False | 1.0 |
| 2 | nan | False | nan |
| 3 | null | True | 0.0 |
NaN survives this fill because it is a floating-point value. A source null takes the fallback.
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 |
|---|---|---|---|---|
| 1 | a | 1 | 10 | 10.0 |
| 2 | a | 2 | null | 13.0 |
| 3 | a | 3 | 16 | 16.0 |
| 4 | b | 1 | null | null |
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.