Sub topic 8
Null versus NaN and missing data
This chapter assumes you know pandas missing-data tools such as isna, fillna and dropna, and that you have read the earlier chapters on expressions and creating columns.
Two kinds of missing in Polars
In pandas, a float column usually stores a missing value as NaN. Polars separates two ideas:
- null means "no value". Every dtype can hold null, including integers, strings and booleans.
- NaN means "not a number". It is a real floating-point value that comes from operations such as
0.0 / 0.0. Only float columns can hold it.
This split has consequences when you port code. A pandas call that treats both as missing may leave one of them untouched in Polars.
import polars as pl
df = pl.DataFrame({"x": [1.0, None, float("nan")]})
df.select(
pl.col("x").is_null().alias("is_null"),
pl.col("x").is_nan().alias("is_nan"),
pl.col("x").fill_null(0.0).alias("filled_null"),
)The result is:
| is_null | is_nan | filled_null |
|---|---|---|
| false | false | 1.0 |
| true | null | 0.0 |
| false | true | NaN |
Three details matter here. is_null is true only for the second row. is_nan is true only for the third row, and it returns null for a null input. fill_null replaces the null but leaves the NaN in place.
Mapping pandas methods
| pandas | Polars | Notes | Column |
|---|---|---|---|
| `s.isna()` | `is_null()` | Add `\ | is_nan()` for floats that may hold NaN. |
| `s.notna()` | `is_not_null()` | Also check `is_not_nan()` for floats. | |
| `s.fillna(0)` | `fill_null(0)` | Does not touch NaN. | |
| `s.fillna(method="ffill")` | `fill_null(strategy="forward")` | Strategies include `backward`, `mean` and `zero`. | |
| `df.dropna()` | `df.drop_nulls()` | Accepts `subset=` as pandas does. | |
| `s.mean()` skipping NaN | `mean()` | Skips null but propagates NaN. |
Handling both together
If your data may contain both, convert NaN to null first. Then every later step needs only the null tools.
import polars as pl
df = pl.DataFrame({"x": [1.0, None, float("nan")]})
clean = df.with_columns(pl.col("x").fill_nan(None))
print(clean.select(pl.col("x").is_null().sum().alias("missing")))
print(clean.select(pl.col("x").mean().alias("mean_x")))fill_nan(None) turns the NaN into null. After that, missing is 2 and mean_x is 1.0, because aggregations skip null.
Without this step, the mean of the original column would be NaN, because NaN is an ordinary float value and spreads through arithmetic.
Common errors
- Calling
fill_nulland expecting NaN to disappear. Usefill_nanor convert to null first. - Filtering with
pl.col("x") != float("nan"). Comparing with NaN does not work as a test. Useis_nan(). - Calling
is_nan()on an integer or string column. It only works on floats. - Reading a CSV where an empty field becomes null, but the text
"NaN"stays a float NaN or a string. Check the dtype after reading. - Expecting integer columns to turn into floats when data is missing. Polars keeps them as integers with null.
Exercise
A float column score contains 1.0, null and NaN. You run df.with_columns(pl.col("score").fill_null(0.0)), then pl.col("score").sum(). What is the sum, and how would you get 1.0?
Show answer
The sum is NaN, because fillnull replaced only the null and the NaN is still in the column. To get 1.0, convert NaN to null first, or fill it directly: `pl.col("score").fillnan(None).fill_null(0.0)`. Then the values are 1.0, 0.0 and 0.0, and the sum is 1.0.