Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for serializing polars DataFrame #7475

Merged
merged 4 commits into from
Jan 20, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading