Skip to content

Commit e0d185a

Browse files
authored
fix: suppress JSONDtypeWarning in Anywidget mode and clean up progress output (#2441)
This PR improves the user experience when using the interactive anywidget display mode (bpd.options.display.repr_mode = "anywidget") by reducing console noise. Verified at: vs code notebook: screen/ACCJRLwyThciMk8 colab notebook: screen/BhNxzpvckYg9Wp8 Fixes #<482120359> 🦕
1 parent 2017cc2 commit e0d185a

5 files changed

Lines changed: 56 additions & 28 deletions

File tree

‎bigframes/__init__.py‎

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,26 @@
1414

1515
"""BigQuery DataFrames provides a DataFrame API scaled by the BigQuery engine."""
1616

17-
frombigframes._configimportoption_context, options
18-
frombigframes._config.bigquery_optionsimportBigQueryOptions
19-
frombigframes.core.global_sessionimportclose_session, get_global_session
20-
importbigframes.enumsasenums
21-
importbigframes.exceptionsasexceptions
22-
frombigframes.sessionimportconnect, Session
23-
frombigframes.versionimport__version__
17+
importwarnings
18+
19+
# Suppress Python version support warnings from google-cloud libraries.
20+
# These are particularly noisy in Colab which still uses Python 3.10.
21+
warnings.filterwarnings(
22+
"ignore",
23+
category=FutureWarning,
24+
message=".*Google will stop supporting.*Python.*",
25+
)
26+
27+
frombigframes._configimportoption_context, options# noqa: E402
28+
frombigframes._config.bigquery_optionsimportBigQueryOptions# noqa: E402
29+
frombigframes.core.global_sessionimport ( # noqa: E402
30+
close_session,
31+
get_global_session,
32+
)
33+
importbigframes.enumsasenums# noqa: E402
34+
importbigframes.exceptionsasexceptions# noqa: E402
35+
frombigframes.sessionimportconnect, Session# noqa: E402
36+
frombigframes.versionimport__version__# noqa: E402
2437

2538
_MAGIC_NAMES= ["bqsql"]
2639

‎bigframes/dataframe.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ def dtypes(self) -> pandas.Series:
332332

333333
@property
334334
defcolumns(self) ->pandas.Index:
335-
returnself.dtypes.index
335+
returnself._block.column_labels
336336

337337
@columns.setter
338338
defcolumns(self, labels: pandas.Index):

‎bigframes/display/anywidget.py‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
importthreading
2424
fromtypingimportAny, Iterator, Optional
2525
importuuid
26+
importwarnings
2627

2728
importpandasaspd
2829

@@ -111,23 +112,32 @@ def __init__(self, dataframe: bigframes.dataframe.DataFrame):
111112
self.page_size=initial_page_size
112113
self.max_columns=initial_max_columns
113114

114-
# TODO(b/469861913): Nested columns from structs (e.g., 'struct_col.name') are not currently sortable.
115-
# TODO(b/463754889): Support non-string column labels for sorting.
116-
ifall(isinstance(col, str) forcolindataframe.columns):
117-
self.orderable_columns= [
118-
str(col_name)
119-
forcol_name, dtypeindataframe.dtypes.items()
120-
ifdtypes.is_orderable(dtype)
121-
]
122-
else:
123-
self.orderable_columns= []
115+
self.orderable_columns=self._get_orderable_columns(dataframe)
124116

125117
self._initial_load()
126118

127119
# Signals to the frontend that the initial data load is complete.
128120
# Also used as a guard to prevent observers from firing during initialization.
129121
self._initial_load_complete=True
130122

123+
def_get_orderable_columns(
124+
self, dataframe: bigframes.dataframe.DataFrame
125+
) ->list[str]:
126+
"""Determine which columns can be used for client-side sorting."""
127+
# TODO(b/469861913): Nested columns from structs (e.g., 'struct_col.name') are not currently sortable.
128+
# TODO(b/463754889): Support non-string column labels for sorting.
129+
ifnotall(isinstance(col, str) forcolindataframe.columns):
130+
return []
131+
132+
withwarnings.catch_warnings():
133+
warnings.simplefilter("ignore", bigframes.exceptions.JSONDtypeWarning)
134+
warnings.simplefilter("ignore", category=FutureWarning)
135+
return [
136+
str(col_name)
137+
forcol_name, dtypeindataframe.dtypes.items()
138+
ifdtypes.is_orderable(dtype)
139+
]
140+
131141
def_initial_load(self) ->None:
132142
"""Get initial data and row count."""
133143
# obtain the row counts

‎bigframes/display/html.py‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,13 @@ def repr_mimebundle(
363363

364364
ifopts.repr_mode=="anywidget":
365365
try:
366-
returnget_anywidget_bundle(obj, include=include, exclude=exclude)
366+
withbigframes.option_context("display.progress_bar", None):
367+
withwarnings.catch_warnings():
368+
warnings.simplefilter(
369+
"ignore", category=bigframes.exceptions.JSONDtypeWarning
370+
)
371+
warnings.simplefilter("ignore", category=FutureWarning)
372+
returnget_anywidget_bundle(obj, include=include, exclude=exclude)
367373
exceptImportError:
368374
# Anywidget is an optional dependency, so warn rather than fail.
369375
# TODO(shuowei): When Anywidget becomes the default for all repr modes,

‎bigframes/pandas/io/api.py‎

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -354,15 +354,14 @@ def _read_gbq_colab(
354354
)
355355
_set_default_session_location_if_possible_deferred_query(create_query)
356356
ifnotconfig.options.bigquery._session_started:
357-
withwarnings.catch_warnings():
358-
# Don't warning about Polars in SQL cell.
359-
# Related to b/437090788.
360-
try:
361-
bigframes._importing.import_polars()
362-
warnings.simplefilter("ignore", bigframes.exceptions.PreviewWarning)
363-
config.options.bigquery.enable_polars_execution=True
364-
exceptImportError:
365-
pass# don't fail if polars isn't available
357+
# Don't warning about Polars in SQL cell.
358+
# Related to b/437090788.
359+
try:
360+
bigframes._importing.import_polars()
361+
warnings.simplefilter("ignore", bigframes.exceptions.PreviewWarning)
362+
config.options.bigquery.enable_polars_execution=True
363+
exceptImportError:
364+
pass# don't fail if polars isn't available
366365

367366
returnglobal_session.with_default_session(
368367
bigframes.session.Session._read_gbq_colab,

0 commit comments

Comments
 (0)