Skip to content
Merged
Show file tree
Hide file tree
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
22 changes: 19 additions & 3 deletions packages/bigframes/bigframes/dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -819,9 +819,25 @@ def __repr__(self) -> str:
column_count=len(self.columns),
)

def _get_display_df_and_blob_cols(self) -> tuple[DataFrame, list[str]]:
"""Process ObjectRef columns for display. (Deprecated)"""
return self, []
def _get_display_df(self) -> DataFrame:
"""Process ObjectRef and JSON/nested JSON columns for display."""
df = self
# Arrow/Pandas to_pandas_batches does not support raw JSON/nested JSON
# columns. Pre-serialize them to string format to bypass this limit.
# Using TO_JSON_STRING via SqlScalarOp handles complex nested STRUCT
# types correctly.
json_cols = [
col
for col in df.columns
if bigframes.dtypes.contains_db_dtypes_json_dtype(df[col].dtype)
]
if json_cols:
op = ops.SqlScalarOp(
_output_type=bigframes.dtypes.STRING_DTYPE,
sql_template="TO_JSON_STRING({0})",
)
df = df.assign(**{col: df[col]._apply_unary_op(op) for col in json_cols})
return df

def _repr_mimebundle_(self, include=None, exclude=None):
"""
Expand Down
26 changes: 6 additions & 20 deletions packages/bigframes/bigframes/display/html.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import bigframes.formatting_helpers as formatter
from bigframes._config import display_options, options
from bigframes.display import plaintext
from bigframes.series import Series

if typing.TYPE_CHECKING:
import bigframes.dataframe
Expand Down Expand Up @@ -191,8 +192,6 @@ def create_html_representation(
total_columns: int,
) -> str:
"""Create an HTML representation of the DataFrame or Series."""
from bigframes.series import Series

opts = options.display
with display_options.pandas_repr(opts):
if isinstance(obj, Series):
Expand All @@ -217,8 +216,6 @@ def create_html_representation(
def _get_obj_metadata(
obj: Union[bigframes.dataframe.DataFrame, bigframes.series.Series],
) -> tuple[bool, bool]:
from bigframes.series import Series

is_series = isinstance(obj, Series)
if is_series:
has_index = len(obj._block.index_columns) > 0
Expand All @@ -237,12 +234,8 @@ def get_anywidget_bundle(
This function encapsulates the logic for anywidget display.
"""
from bigframes import display
from bigframes.series import Series

if isinstance(obj, Series):
df = obj.to_frame()
else:
df, _ = obj._get_display_df_and_blob_cols()
df = obj._get_display_df()

widget = display.TableWidget(df)
widget_repr_result = widget._repr_mimebundle_(include=include, exclude=exclude)
Expand Down Expand Up @@ -290,18 +283,11 @@ def repr_mimebundle_deferred(
def repr_mimebundle_head(
obj: Union[bigframes.dataframe.DataFrame, bigframes.series.Series],
) -> dict[str, str]:
from bigframes.series import Series

opts = options.display
if isinstance(obj, Series):
pandas_df, row_count, query_job = obj._block.retrieve_repr_request_results(
opts.max_rows
)
else:
df, _ = obj._get_display_df_and_blob_cols()
pandas_df, row_count, query_job = df._block.retrieve_repr_request_results(
opts.max_rows
)
df = obj._get_display_df()
pandas_df, row_count, query_job = df._block.retrieve_repr_request_results(
opts.max_rows
)

obj._set_internal_query_job(query_job)
column_count = len(pandas_df.columns)
Expand Down
3 changes: 3 additions & 0 deletions packages/bigframes/bigframes/series.py
Original file line number Diff line number Diff line change
Expand Up @@ -572,6 +572,9 @@ def reset_index(
block = block.assign_label(self._value_column, name)
return bigframes.dataframe.DataFrame(block)

def _get_display_df(self) -> bigframes.dataframe.DataFrame:
return self.to_frame()._get_display_df()

def _repr_mimebundle_(self, include=None, exclude=None):
"""
Custom display method for IPython/Jupyter environments.
Expand Down
Loading
Loading