Skip to content

Commit 533541a

Browse files
committed
Fix: Handle JSON dtype in anywidget display
This commit fixes an AttributeError that occurred when displaying a DataFrame with a JSON column in anywidget mode. The dtype check was incorrect and has been updated. Additionally, the SQL compilation for casting JSON to string has been corrected to use TO_JSON_STRING.
1 parent 5cf909c commit 533541a

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

bigframes/core/compile/ibis_compiler/scalar_op_registry.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ def astype_op_impl(x: ibis_types.Value, op: ops.AsTypeOp):
10361036
if to_type == ibis_dtypes.bool:
10371037
return cast_json_to_bool_in_safe(x) if op.safe else cast_json_to_bool(x)
10381038
if to_type == ibis_dtypes.string:
1039-
return cast_json_to_string_in_safe(x) if op.safe else cast_json_to_string(x)
1039+
return to_json_string(x)
10401040

10411041
# TODO: either inline this function, or push rest of this op into the function
10421042
return bigframes.core.compile.ibis_types.cast_ibis_value(x, to_type, safe=op.safe)

bigframes/display/anywidget.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,20 @@ def __init__(self, dataframe: bigframes.dataframe.DataFrame):
6262
)
6363

6464
super().__init__()
65-
self._dataframe = dataframe
65+
# Workaround for Arrow bug https://github.com/apache/arrow/issues/45262
66+
# JSON columns are not supported in `to_pandas_batches` and will be converted to string.
67+
json_cols = [
68+
col
69+
for col, dtype in dataframe.dtypes.items()
70+
if dtype == bigframes.dtypes.JSON_DTYPE
71+
]
72+
if json_cols:
73+
df_copy = dataframe.copy()
74+
for col in json_cols:
75+
df_copy[str(col)] = df_copy[str(col)].astype("string")
76+
self._dataframe = df_copy
77+
else:
78+
self._dataframe = dataframe
6679

6780
# Initialize attributes that might be needed by observers FIRST
6881
self._table_id = str(uuid.uuid4())
@@ -191,9 +204,6 @@ def _reset_batches_for_new_page_size(self):
191204

192205
def _set_table_html(self):
193206
"""Sets the current html data based on the current page and page size."""
194-
# TODO (shuowei): BigFrames Series with db_dtypes.JSONArrowType column
195-
# fails to convert to pandas DataFrame in anywidget environment due to
196-
# missing handling in to_pandas_batches(). b/453561268
197207
# For empty dataframe, render empty table with headers.
198208
if self.row_count == 0:
199209
page_data = self._cached_data

0 commit comments

Comments
 (0)