pythonpython-polars

Polars Schema: TypeError: dtypes must be fully-specified, got: Datetime


Hi I want to define a polars schema.

It works fine without a datetime format. However it fails with pl.Datetime.

import polars as pl

testing_schema: pl.Schema = pl.Schema(
    {
        "date": pl.Datetime,
        "some_int": pl.Int64,
        "some_str": pl.Utf8,
        "some_cost": pl.Float64,
    },
)

The error:

lib/python3.11/site-packages/polars/schema.py", line 47, in _check_dtype
    raise TypeError(msg)
TypeError: dtypes must be fully-specified, got: Datetime

Unfortunately I do not have any idea on how to fix it.


Solution

  • If you look at pl.Datetime, you'll find that it is initialized with parameters:

    class polars.datatypes.Datetime(
        time_unit: TimeUnit = 'us', time_zone: str | timezone | None = None
    )
    

    Unlike, say, pl.Int64. Hence, you need to add the parentheses at the end, (), to use the defaults, or to pass arguments and override them.

    import polars as pl
    
    testing_schema: pl.Schema = pl.Schema(
        {
            "date": pl.Datetime(),
            "date2": pl.Datetime(time_unit="ns", time_zone="UTC"),
            "some_int": pl.Int64,
        },
    )