@@ -376,6 +376,92 @@ def test_to_pandas_batches_w_empty_dataframe(session): |
376 | 376 | pandas.testing.assert_series_equal(results[0].dtypes, empty.dtypes) |
377 | 377 |
|
378 | 378 |
|
| 379 | +@pytest.mark.skipif( |
| 380 | + bigframes.features.PANDAS_VERSIONS.is_arrow_list_dtype_usable, |
| 381 | + reason="Test for pandas 1.x behavior only", |
| 382 | +) |
| 383 | +def test_to_pandas_batches_preserves_dtypes_for_populated_nested_json_pandas1(session): |
| 384 | + """Verifies to_pandas_batches() preserves dtypes for nested JSON in pandas 1.x.""" |
| 385 | + sql = """ |
| 386 | + SELECT |
| 387 | + 0 AS id, |
| 388 | + [JSON '{"a":1}', JSON '{"b":2}'] AS json_array, |
| 389 | + STRUCT(JSON '{"x":1}' AS json_field, 'test' AS str_field) AS json_struct |
| 390 | + """ |
| 391 | + df = session.read_gbq(sql, index_col="id") |
| 392 | + batches = list(df.to_pandas_batches()) |
| 393 | + |
| 394 | + assert batches[0].dtypes["json_array"] == "object" |
| 395 | + assert isinstance(batches[0].dtypes["json_struct"], pd.ArrowDtype) |
| 396 | + |
| 397 | + |
| 398 | +@pytest.mark.skipif( |
| 399 | + not bigframes.features.PANDAS_VERSIONS.is_arrow_list_dtype_usable, |
| 400 | + reason="Test for pandas 2.x behavior only", |
| 401 | +) |
| 402 | +def test_to_pandas_batches_preserves_dtypes_for_populated_nested_json_pandas2(session): |
| 403 | + """Verifies to_pandas_batches() preserves dtypes for nested JSON in pandas 2.x.""" |
| 404 | + sql = """ |
| 405 | + SELECT |
| 406 | + 0 AS id, |
| 407 | + [JSON '{"a":1}', JSON '{"b":2}'] AS json_array, |
| 408 | + STRUCT(JSON '{"x":1}' AS json_field, 'test' AS str_field) AS json_struct |
| 409 | + """ |
| 410 | + df = session.read_gbq(sql, index_col="id") |
| 411 | + batches = list(df.to_pandas_batches()) |
| 412 | + |
| 413 | + assert isinstance(batches[0].dtypes["json_array"], pd.ArrowDtype) |
| 414 | + assert isinstance(batches[0].dtypes["json_array"].pyarrow_dtype, pa.ListType) |
| 415 | + assert isinstance(batches[0].dtypes["json_struct"], pd.ArrowDtype) |
| 416 | + |
| 417 | + |
| 418 | +@pytest.mark.skipif( |
| 419 | + bigframes.features.PANDAS_VERSIONS.is_arrow_list_dtype_usable, |
| 420 | + reason="Test for pandas 1.x behavior only", |
| 421 | +) |
| 422 | +def test_to_pandas_batches_should_not_error_on_empty_nested_json_pandas1(session): |
| 423 | + """Verify to_pandas_batches() works with empty nested JSON types in pandas 1.x.""" |
| 424 | + |
| 425 | + sql = """ |
| 426 | + SELECT |
| 427 | + 1 AS id, |
| 428 | + [] AS json_array, |
| 429 | + STRUCT(NULL AS json_field, 'test2' AS str_field) AS json_struct |
| 430 | + """ |
| 431 | + df = session.read_gbq(sql, index_col="id") |
| 432 | + |
| 433 | + # The main point: this should not raise an error |
| 434 | + batches = list(df.to_pandas_batches()) |
| 435 | + assert sum(len(b) for b in batches) == 1 |
| 436 | + |
| 437 | + assert batches[0].dtypes["json_array"] == "object" |
| 438 | + assert isinstance(batches[0].dtypes["json_struct"], pd.ArrowDtype) |
| 439 | + |
| 440 | + |
| 441 | +@pytest.mark.skipif( |
| 442 | + not bigframes.features.PANDAS_VERSIONS.is_arrow_list_dtype_usable, |
| 443 | + reason="Test for pandas 2.x behavior only", |
| 444 | +) |
| 445 | +def test_to_pandas_batches_should_not_error_on_empty_nested_json_pandas2(session): |
| 446 | + """Verify to_pandas_batches() works with empty nested JSON types in pandas 2.x.""" |
| 447 | + |
| 448 | + sql = """ |
| 449 | + SELECT |
| 450 | + 1 AS id, |
| 451 | + [] AS json_array, |
| 452 | + STRUCT(NULL AS json_field, 'test2' AS str_field) AS json_struct |
| 453 | + """ |
| 454 | + df = session.read_gbq(sql, index_col="id") |
| 455 | + |
| 456 | + # The main point: this should not raise an error |
| 457 | + batches = list(df.to_pandas_batches()) |
| 458 | + assert sum(len(b) for b in batches) == 1 |
| 459 | + |
| 460 | + assert isinstance(batches[0].dtypes["json_array"], pd.ArrowDtype) |
| 461 | + assert isinstance(batches[0].dtypes["json_struct"], pd.ArrowDtype) |
| 462 | + assert isinstance(batches[0].dtypes["json_struct"].pyarrow_dtype, pa.StructType) |
| 463 | + |
| 464 | + |
379 | 465 | @pytest.mark.parametrize("allow_large_results", (True, False)) |
380 | 466 | def test_to_pandas_batches_w_page_size_and_max_results(session, allow_large_results): |
381 | 467 | """Verify to_pandas_batches() APIs returns the expected page size. |
|