1414
1515from typing import Tuple
1616
17- import db_dtypes # type:ignore
1817import google .api_core .exceptions
1918import pandas as pd
2019import pandas .testing
@@ -307,10 +306,10 @@ def test_load_json_w_json_string_items(session):
307306 )
308307 ),
309308 """
310- df = session .read_gbq (sql , index_col = "id" )
311-
312- assert df .dtypes ["json_col" ] == pd .ArrowDtype (db_dtypes .JSONArrowType ())
309+ # TODO(b/401630655): JSON is not compatible with allow_large_results=False
310+ df = session .read_gbq (sql , index_col = "id" ).to_pandas (allow_large_results = True )
313311
312+ assert dtypes .is_json_type (df .dtypes ["json_col" ])
314313 assert df ["json_col" ][0 ] == '{"boolean":true}'
315314 assert df ["json_col" ][1 ] == '{"int":100}'
316315 assert df ["json_col" ][2 ] == '{"float":0.98}'
@@ -325,17 +324,24 @@ def test_load_json_w_json_string_items(session):
325324
326325def test_load_json_to_pandas_has_correct_result (session ):
327326 df = session .read_gbq ("SELECT JSON_OBJECT('foo', 10, 'bar', TRUE) AS json_col" )
328- assert df .dtypes ["json_col" ] == pd .ArrowDtype (db_dtypes .JSONArrowType ())
329- result = df .to_pandas ()
327+ assert dtypes .is_json_type (df .dtypes ["json_col" ])
328+
329+ # TODO(b/401630655): JSON is not compatible with allow_large_results=False
330+ result = df .to_pandas (allow_large_results = True )
330331
331332 # These JSON strings are compatible with BigQuery's JSON storage,
332333 pd_df = pd .DataFrame (
333334 {"json_col" : ['{"bar":true,"foo":10}' ]},
334- dtype = pd . ArrowDtype ( db_dtypes . JSONArrowType ()) ,
335+ dtype = dtypes . JSON_DTYPE ,
335336 )
336337 pd_df .index = pd_df .index .astype ("Int64" )
337- pd .testing .assert_series_equal (result .dtypes , pd_df .dtypes )
338- pd .testing .assert_series_equal (result ["json_col" ], pd_df ["json_col" ])
338+ assert dtypes .is_json_type (pd_df .dtypes ["json_col" ])
339+
340+ # `check_exact=False` can workaround the known issue in pandas:
341+ # https://github.com/pandas-dev/pandas/issues/60958
342+ pd .testing .assert_series_equal (
343+ result ["json_col" ], pd_df ["json_col" ], check_exact = False
344+ )
339345
340346
341347def test_load_json_in_struct (session ):
@@ -363,13 +369,14 @@ def test_load_json_in_struct(session):
363369 )
364370 ), 7),
365371 """
366- df = session .read_gbq (sql , index_col = "id" )
372+ # TODO(b/401630655): JSON is not compatible with allow_large_results=False
373+ df = session .read_gbq (sql , index_col = "id" ).to_pandas (allow_large_results = True )
367374
368375 assert isinstance (df .dtypes ["struct_col" ], pd .ArrowDtype )
369376 assert isinstance (df .dtypes ["struct_col" ].pyarrow_dtype , pa .StructType )
370377
371378 data = df ["struct_col" ].struct .field ("data" )
372- assert data . dtype == pd . ArrowDtype ( db_dtypes . JSONArrowType () )
379+ assert dtypes . is_json_type ( data . dtype )
373380
374381 assert data [0 ] == '{"boolean":true}'
375382 assert data [1 ] == '{"int":100}'
@@ -400,14 +407,15 @@ def test_load_json_in_array(session):
400407 )
401408 ] AS array_col,
402409 """
403- df = session .read_gbq (sql , index_col = "id" )
410+ # TODO(b/401630655): JSON is not compatible with allow_large_results=False
411+ df = session .read_gbq (sql , index_col = "id" ).to_pandas (allow_large_results = True )
404412
405413 assert isinstance (df .dtypes ["array_col" ], pd .ArrowDtype )
406414 assert isinstance (df .dtypes ["array_col" ].pyarrow_dtype , pa .ListType )
407415
408416 data = df ["array_col" ].list
409417 assert data .len ()[0 ] == 7
410- assert data [0 ].dtype == pd . ArrowDtype ( db_dtypes . JSONArrowType () )
418+ assert dtypes . is_json_type ( data [0 ].dtype )
411419
412420 assert data [0 ][0 ] == '{"boolean":true}'
413421 assert data [1 ][0 ] == '{"int":100}'
0 commit comments