# Missing data
source: https://docs.chalk.ai/docs/chalkdf/expressions/missing-data

## 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.

### Find missing values

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

```python
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()
```

| id · `int64` | estimate · `int64` |
| ------------ | ------------------ |
| 2            | 8                  |
| 4            | null               |

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.

```python
tx.project({
    "id": _.id, "reported": _.amount,
    "best_available": F.coalesce(_.amount, _.estimate),
    "display_amount": F.coalesce(_.amount, _.estimate, 0),
}).run()
```

| id · `int64` | reported · `int64` | best_available · `int64` | display_amount · `int64` |
| ------------ | ------------------ | ------------------------ | ------------------------ |
| 1            | 12                 | 12                       | 12                       |
| 2            | null               | 8                        | 8                        |
| 3            | 0                  | 0                        | 0                        |
| 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.

### 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.

```python
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()
```

| rows · `int64` | reported · `int64` | observed_mean · `double` | zero_filled_mean · `double` |
| -------------- | ------------------ | ------------------------ | --------------------------- |
| 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.

### Check floating-point NaN separately

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

```python
samples = DataFrame({"value": [1.0, float("nan"), None]})
samples.project({
    "value": _.value, "missing": F.is_null(_.value),
    "filled": F.coalesce(_.value, 0.0),
}).run()
```

| value · `double` | missing · `bool` | filled · `double` |
| ---------------- | ---------------- | ----------------- |
| 1.0              | False            | 1.0               |
| nan              | False            | nan               |
| null             | True             | 0.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.

```python
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()
```

| sensor · `string` | minute · `int64` | observed · `int64` | estimated · `double` |
| ----------------- | ---------------- | ------------------ | -------------------- |
| a                 | 1                | 10                 | 10.0                 |
| a                 | 2                | null               | 13.0                 |
| a                 | 3                | 16                 | 16.0                 |
| 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](/docs/chalkdf/expressions/casting) and [aggregations](/docs/chalkdf/expressions/aggregations).





