-
-
Notifications
You must be signed in to change notification settings - Fork 521
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
Support Polars as parameter of ReactiveESM component #7468
Comments
SolutionTo support a custom import panel as pn
import param
from panel.custom import JSComponent
import polars as pl
import pandas as pd
from panel_gwalker._tabular_data import TabularData
from panel.io.datamodel import PARAM_MAPPING, bp
pn.extension()
_VALID_CLASSES = (
"<class 'pandas.core.frame.DataFrame'>",
"<class 'polars.dataframe.frame.DataFrame'>",
)
class TabularData(param.Parameter):
def _validate(self, val):
super()._validate(val=val)
try:
if str(val.__class__) in _VALID_CLASSES:
return
except:
pass
msg=f"A value of type '{type(val)}' is not valid"
raise ValueError(msg)
def _column_datasource_from_polars_df(df):
df = df.to_pandas()
return ColumnDataSource._data_from_df(df)
PARAM_MAPPING.update({
TabularData: lambda p, kwargs: (
bp.ColumnData(bp.Any, bp.Seq(bp.Any), **kwargs),
[(bp.PandasDataFrame, _column_datasource_from_polars_df)],
),
})
class GraphicWalker(JSComponent):
object = param.DataFrame()
_esm = '''
export function render({ model, el }) {
console.log(model.object)
el.innerHTML = JSON.stringify(model.object)
}
'''
GraphicWalker(object=pd.DataFrame({"x": [1,2,3]})).servable() |
I can see that class PandasDataFrame(Property["DataFrame"]):
""" Accept Pandas 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 pandas as pd
if isinstance(value, pd.DataFrame):
return
msg = "" if not detail else f"expected Pandas DataFrame, got {value!r}"
raise ValueError(msg) Someday Bokeh should probably support a more general Any plans about more general support for dataframes @mattpap? |
There's an ongoing discussion regarding this subject in bokeh/bokeh#13780. |
I will apply a temporary fix that will convert polars DataFrames to pandas for now. |
Would it be an idea to finalize the proof of concept in https://github.com/panel-extensions/panel-graphic-walker/blob/89d6bac66d119d6ad1f75dfe27ec984d18d895ed/src/panel_gwalker/_tabular_data.py#L1 first. To me it looks very close to the right solution. Then apply in panel? What is missing is usage of narwhals to enable any tabular/ dataframe like data source. Not just polars. |
Started in Param: holoviz/param#975 |
panel==1.5.3
I would like to add support for Polars DataFrame to
GraphicWalker.object
. Its unclear to me whether this is supported and how. I guess special care is taken about pandas dataframe serialization? Please support it.The text was updated successfully, but these errors were encountered: