filterpython-polars

How to filter for greater than on a datetime column while including Nulls?


I have a dataframe df with a datetime column (Datetime(time_unit='us', time_zone=None),) This column can contain Null values.

How can I filter this dataframe while keeping null values?

df.filter(pl.col("end_date") >= datetime(2024, 8, 31))

does not include Null values.

df.filter(pl.col("end_date") >= datetime(2023, 8, 31)) | pl.col("end_date").is_null())

results in this error message:

InvalidOperationError: bitor operation not supported for dtype datetime[μs]


Solution

  • You just need to add brackets to specify order of applying operations:

    df.filter(
        (pl.col.end_date >= datetime(2024, 8, 31)) |
        pl.col.end_date.is_null()
    )