Skip to content

Commit

Permalink
Add support for serializing polars DataFrame (#7475)
Browse files Browse the repository at this point in the history
* Add support for serializing polars DataFrame

* Apply suggestions from code review

* Apply suggestions from code review
  • Loading branch information
philippjfr authored Jan 20, 2025
1 parent 64e9992 commit 838fcc3
Showing 1 changed file with 33 additions and 1 deletion.
34 changes: 33 additions & 1 deletion panel/io/datamodel.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from __future__ import annotations

import sys

from functools import partial
from typing import Any
from weakref import WeakKeyDictionary

import bokeh
Expand Down Expand Up @@ -35,6 +38,26 @@ def validate(self, value, detail=True):
raise ValueError(msg)


class PolarsDataFrame(bokeh.core.property.bases.Property):
""" Accept Polars DataFrame values.
This property only exists to support type validation, e.g. for "accepts"
clauses. It is not serializable itself, and is not useful to add to
Bokeh models directly.
"""

def validate(self, value: Any, detail: bool = True) -> None:
super().validate(value, detail)

import polars as pl
if isinstance(value, (pl.DataFrame, pl.LazyFrame)):
return

msg = "" if not detail else f"expected Pandas DataFrame, got {value!r}"
raise ValueError(msg)


class ParameterizedList(bokeh.core.property.bases.Property):
""" Accept a list of Parameterized objects.
Expand Down Expand Up @@ -88,6 +111,15 @@ def bytes_param(p, kwargs):
kwargs['default'] = None
return bp.Nullable(bp.Bytes, **kwargs)

def df_to_dict(df):
if 'polars' in sys.modules:
import polars as pl
if isinstance(df, pl.LazyFrame):
df = df.collect()
if isinstance(df, pl.DataFrame):
df = df.to_pandas()
return ColumnDataSource._data_from_df(df)

PARAM_MAPPING = {
pm.Array: lambda p, kwargs: bp.Array(bp.Any, **kwargs),
pm.Boolean: lambda p, kwargs: bp.Bool(**kwargs),
Expand All @@ -98,7 +130,7 @@ def bytes_param(p, kwargs):
pm.Color: color_param_to_ppt,
pm.DataFrame: lambda p, kwargs: (
bp.ColumnData(bp.Any, bp.Seq(bp.Any), **kwargs),
[(bp.PandasDataFrame, lambda x: ColumnDataSource._data_from_df(x))]
[(bp.PandasDataFrame, df_to_dict), (PolarsDataFrame, df_to_dict)]
),
pm.DateRange: lambda p, kwargs: bp.Tuple(bp.Datetime, bp.Datetime, **kwargs),
pm.Date: lambda p, kwargs: bp.Datetime(**kwargs),
Expand Down

0 comments on commit 838fcc3

Please sign in to comment.