Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement type hinting for DataFrame

I would like to have same sort of Python type hinting on Polars DataFrame. Since DataFrame is not a generic type, I cannot do DataFrame[MyFrameSchema].

For example:

MyFrameSchema = {'price': float, 'quantity': int, 'product_name': str}

or:

class MyDataFrame(pl.DataFrame)
    ...

I now Polars has type SchemaDict in DataFrame constructor from polars.type_aliases import SchemaDict, but I don't know how to use it in Python code.

Maybe someone has idea how to wrap DataFrame into TMyDataFrame Something like TMyDataFrame: TypeAlias = ... or something else. I am open for suggestions.

like image 391
Joris Medeišis Avatar asked Dec 31 '25 20:12

Joris Medeišis


1 Answers

There are two Polars DataFrame validators that I know of, pandera (https://github.com/unionai-oss/pandera) and patito (https://github.com/JakobGM/patito). However, these do runtime validation and aren't really type hints.

like image 157
stressed Avatar answered Jan 03 '26 08:01

stressed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!