|
| 1 | +import datetime |
| 2 | +import decimal |
1 | 3 | import json |
2 | 4 | import logging |
3 | 5 | import os |
4 | 6 | import sys |
5 | 7 | from pathlib import Path |
| 8 | +from typing import Any, Union |
6 | 9 |
|
7 | 10 | import narwhals as nw |
8 | 11 | import pandas as pd |
|
11 | 14 | from narwhals.dataframe import LazyFrame |
12 | 15 | from narwhals.typing import FrameT |
13 | 16 |
|
| 17 | + |
| 18 | +def cast_to_supported_dtypes(df: pd.DataFrame, sample: int = 100) -> pd.DataFrame: |
| 19 | + """ |
| 20 | + Convert decimal.Decimal to float in a pandas DataFrame, as |
| 21 | + Bokeh ColumnDataSource does not support decimal.Decimal. |
| 22 | + Samples only a subset of the DataFrame to check for decimal.Decimal |
| 23 | +
|
| 24 | + Arguments |
| 25 | + --------- |
| 26 | + df (pd.DataFrame): |
| 27 | + the DataFrame to convert |
| 28 | + sample (int): |
| 29 | + number of rows to sample to check for decimal.Decimal |
| 30 | + """ |
| 31 | + df = df.copy() |
| 32 | + for col in df.select_dtypes(include=["object"]).columns: |
| 33 | + df_col_sample = df[col].sample(min(sample, len(df))) |
| 34 | + try: |
| 35 | + if df_col_sample.apply(lambda x: isinstance(x, decimal.Decimal)).any(): |
| 36 | + df[col] = pd.to_numeric(df[col], errors="coerce") |
| 37 | + if df_col_sample.apply( |
| 38 | + lambda x: isinstance(x, (datetime.datetime, datetime.date)) |
| 39 | + ).any(): |
| 40 | + df[col] = pd.to_datetime(df[col], errors="coerce") |
| 41 | + except Exception: |
| 42 | + df[col] = df[col].astype(str) |
| 43 | + return df |
| 44 | + |
| 45 | + |
14 | 46 | logger = logging.getLogger("panel-graphic-walker") |
15 | 47 | FORMAT = "%(asctime)s | %(levelname)s | %(name)s | %(message)s" |
16 | 48 | from narwhals.typing import FrameT |
@@ -90,7 +122,7 @@ def _raw_fields(data: FrameT) -> list[dict]: |
90 | 122 | except Exception as ex: |
91 | 123 | pass |
92 | 124 |
|
93 | | - pandas_data = data.to_pandas() |
| 125 | + pandas_data = cast_to_supported_dtypes(data.to_pandas()) |
94 | 126 | return _raw_fields_core(pandas_data) |
95 | 127 |
|
96 | 128 |
|
|
0 commit comments