mwouts / itables

Pandas DataFrames as Interactive DataTables
https://mwouts.github.io/itables/
MIT License
765 stars 56 forks source link

Support for Polars Struct #290

Open Samox1 opened 3 months ago

Samox1 commented 3 months ago

I ran into a problem with the Polars library and their approach to the Dict = Struct type (in Polars). A simple cast to String takes care of it, but I prefer to ask if there is a possibility to add functionality to show a column with type Struct without error.

What appears now (in IPYNB file): image

Example:

kappa = polars.DataFrame(['id_1','id_1','id_2','id_1','id_4','id_3','id_1','id_2','id_1'],
                        schema={'col_1'}).select(polars.col('col_1').value_counts())
kappa

Returns: image

with itables:

show(kappa)

image

Adding casting to String:

kappa = polars.DataFrame(['id_1','id_1','id_2','id_1','id_4','id_3','id_1','id_2','id_1'],
                        schema={'col_1'}).select(polars.col('col_1').value_counts().cast(polars.String))

itables returns: image

mwouts commented 3 months ago

Hi @Samox1 , thank you for pointing out this issue (and for sharing a reproducible example, that's great!). I will look into it soon.

mwouts commented 3 months ago

Hi @Samox1 , I have a tentative PR, which you can try following these instructions.

Interestingly the reason why we see Object shown by DataTables is that the data passed to the table (a two dimensional array) contains dicts (which are converted to javascript maps). In my PR I am now replacing these dicts with their str representation, but so far the result is different from the default rendering by Polars so the PR might need a bit more work.

We could possibly use a cast as you do above, or look into the code that polars use to display these special dataframes.

image