Skip to content

Commit cc6dd64

Browse files
committed
move helper function to dtypes.py
1 parent 9c10962 commit cc6dd64

File tree

3 files changed

+54
-36
lines changed

3 files changed

+54
-36
lines changed

bigframes/core/blocks.py

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import warnings
4444

4545
import bigframes_vendored.constants as constants
46-
import db_dtypes
4746
import google.cloud.bigquery as bigquery
4847
import numpy
4948
import pandas as pd
@@ -135,21 +134,6 @@ class MaterializationOptions:
135134
ordered: bool = True
136135

137136

138-
def _replace_json_arrow_with_string(pa_type: pa.DataType) -> pa.DataType:
139-
"""Recursively replace JSONArrowType with string type."""
140-
if isinstance(pa_type, db_dtypes.JSONArrowType):
141-
return pa.string()
142-
if isinstance(pa_type, pa.ListType):
143-
return pa.list_(_replace_json_arrow_with_string(pa_type.value_type))
144-
if isinstance(pa_type, pa.StructType):
145-
new_fields = [
146-
field.with_type(_replace_json_arrow_with_string(field.type))
147-
for field in pa_type
148-
]
149-
return pa.struct(new_fields)
150-
return pa_type
151-
152-
153137
class Block:
154138
"""A immutable 2D data structure."""
155139

@@ -746,7 +730,9 @@ def to_pandas_batches(
746730
# MyPy doesn't automatically narrow the type of 'dtype' here,
747731
# so we add an explicit check.
748732
if isinstance(dtype, pd.ArrowDtype):
749-
safe_pa_type = _replace_json_arrow_with_string(dtype.pyarrow_dtype)
733+
safe_pa_type = bigframes.dtypes._replace_json_arrow_with_string(
734+
dtype.pyarrow_dtype
735+
)
750736
safe_dtype = pd.ArrowDtype(safe_pa_type)
751737
series_map[col] = pd.Series([], dtype=safe_dtype).astype(dtype)
752738
else:

bigframes/dtypes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -954,6 +954,21 @@ def contains_db_dtypes_json_dtype(dtype):
954954
return contains_db_dtypes_json_arrow_type(dtype.pyarrow_dtype)
955955

956956

957+
def _replace_json_arrow_with_string(pa_type: pa.DataType) -> pa.DataType:
958+
"""Recursively replace JSONArrowType with string type."""
959+
if isinstance(pa_type, db_dtypes.JSONArrowType):
960+
return pa.string()
961+
if isinstance(pa_type, pa.ListType):
962+
return pa.list_(_replace_json_arrow_with_string(pa_type.value_type))
963+
if isinstance(pa_type, pa.StructType):
964+
new_fields = [
965+
field.with_type(_replace_json_arrow_with_string(field.type))
966+
for field in pa_type
967+
]
968+
return pa.struct(new_fields)
969+
return pa_type
970+
971+
957972
def warn_on_db_dtypes_json_dtype(dtypes):
958973
"""Warn that the JSON dtype is changing.
959974

notebooks/dataframes/anywidget_mode.ipynb

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@
151151
{
152152
"data": {
153153
"application/vnd.jupyter.widget-view+json": {
154-
"model_id": "93dd10072d564a02a0278817d14855a9",
154+
"model_id": "47795eaa10f149aeb99574232c0936eb",
155155
"version_major": 2,
156156
"version_minor": 1
157157
},
@@ -214,16 +214,17 @@
214214
{
215215
"data": {
216216
"application/vnd.jupyter.widget-view+json": {
217-
"model_id": "6e2538d446e344ac8505e4706730243e",
217+
"model_id": "8354ce0f82d3495a9b630dfc362f73ee",
218218
"version_major": 2,
219219
"version_minor": 1
220220
},
221221
"text/plain": [
222222
"TableWidget(page_size=10, row_count=5552452, table_html='<table border=\"1\" class=\"dataframe table table-stripe…"
223223
]
224224
},
225+
"execution_count": 7,
225226
"metadata": {},
226-
"output_type": "display_data"
227+
"output_type": "execute_result"
227228
}
228229
],
229230
"source": [
@@ -292,8 +293,27 @@
292293
{
293294
"data": {
294295
"text/html": [
295-
"✅ Completed. \n",
296-
" Query processed 171.4 MB in a moment of slot time.\n",
296+
"\n",
297+
" Query started with request ID bigframes-dev:US.c45952fb-01b4-409c-9da4-f7c5bfc0d47d.<details><summary>SQL</summary><pre>SELECT\n",
298+
"`state` AS `state`,\n",
299+
"`gender` AS `gender`,\n",
300+
"`year` AS `year`,\n",
301+
"`name` AS `name`,\n",
302+
"`number` AS `number`\n",
303+
"FROM\n",
304+
"(SELECT\n",
305+
" *\n",
306+
"FROM (\n",
307+
" SELECT\n",
308+
" `state`,\n",
309+
" `gender`,\n",
310+
" `year`,\n",
311+
" `name`,\n",
312+
" `number`\n",
313+
" FROM `bigquery-public-data.usa_names.usa_1910_2013` FOR SYSTEM_TIME AS OF TIMESTAMP(&#x27;2025-10-30T21:48:48.979701+00:00&#x27;)\n",
314+
") AS `t0`)\n",
315+
"ORDER BY `name` ASC NULLS LAST ,`year` ASC NULLS LAST ,`state` ASC NULLS LAST\n",
316+
"LIMIT 5</pre></details>\n",
297317
" "
298318
],
299319
"text/plain": [
@@ -313,16 +333,17 @@
313333
{
314334
"data": {
315335
"application/vnd.jupyter.widget-view+json": {
316-
"model_id": "d6faf367ea5d44ad9d275506d870557a",
336+
"model_id": "59461286a17d4a42b6be6d9d9c7bf7e3",
317337
"version_major": 2,
318338
"version_minor": 1
319339
},
320340
"text/plain": [
321341
"TableWidget(page_size=10, row_count=5, table_html='<table border=\"1\" class=\"dataframe table table-striped tabl…"
322342
]
323343
},
344+
"execution_count": 9,
324345
"metadata": {},
325-
"output_type": "display_data"
346+
"output_type": "execute_result"
326347
}
327348
],
328349
"source": [
@@ -352,7 +373,7 @@
352373
"data": {
353374
"text/html": [
354375
"✅ Completed. \n",
355-
" Query processed 85.9 kB in 24 seconds of slot time.\n",
376+
" Query processed 85.9 kB in 14 seconds of slot time.\n",
356377
" "
357378
],
358379
"text/plain": [
@@ -366,20 +387,16 @@
366387
"name": "stderr",
367388
"output_type": "stream",
368389
"text": [
369-
"/usr/local/google/home/shuowei/src/github.com/googleapis/python-bigquery-dataframes/bigframes/dtypes.py:969: JSONDtypeWarning: JSON columns will be represented as pandas.ArrowDtype(pyarrow.json_())\n",
390+
"/usr/local/google/home/shuowei/src/python-bigquery-dataframes/bigframes/dtypes.py:969: JSONDtypeWarning: JSON columns will be represented as pandas.ArrowDtype(pyarrow.json_())\n",
370391
"instead of using `db_dtypes` in the future when available in pandas\n",
371392
"(https://github.com/pandas-dev/pandas/issues/60958) and pyarrow.\n",
372-
" warnings.warn(msg, bigframes.exceptions.JSONDtypeWarning)\n",
373-
"/usr/local/google/home/shuowei/src/github.com/googleapis/python-bigquery-dataframes/bigframes/dataframe.py:867: UserWarning: Converting JSON columns to strings for display. This is temporary and will be removed when the frontend supports JSON types.\n",
374-
" warnings.warn(\n"
393+
" warnings.warn(msg, bigframes.exceptions.JSONDtypeWarning)\n"
375394
]
376395
},
377396
{
378397
"data": {
379398
"text/html": [
380-
"✅ Completed. \n",
381-
" Query processed 0 Bytes in a moment of slot time.\n",
382-
" "
399+
"✅ Completed. "
383400
],
384401
"text/plain": [
385402
"<IPython.core.display.HTML object>"
@@ -391,9 +408,9 @@
391408
{
392409
"data": {
393410
"application/vnd.jupyter.widget-view+json": {
394-
"model_id": "b6d6f3bacc2c43fc9a335e6039db12a5",
411+
"model_id": "d1794b42579542a8980bd158e521bd3e",
395412
"version_major": 2,
396-
"version_minor": 0
413+
"version_minor": 1
397414
},
398415
"text/plain": [
399416
"TableWidget(page_size=10, row_count=5, table_html='<table border=\"1\" class=\"dataframe table table-striped tabl…"
@@ -430,7 +447,7 @@
430447
],
431448
"metadata": {
432449
"kernelspec": {
433-
"display_name": "3.10.18",
450+
"display_name": "venv",
434451
"language": "python",
435452
"name": "python3"
436453
},
@@ -444,7 +461,7 @@
444461
"name": "python",
445462
"nbconvert_exporter": "python",
446463
"pygments_lexer": "ipython3",
447-
"version": "3.10.18"
464+
"version": "3.10.15"
448465
}
449466
},
450467
"nbformat": 4,

0 commit comments

Comments
 (0)