GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — fix: support results with STRUCT and ARRAY columns containing JSON subfields in `to_pandas_batches()` by shuoweil · Pull Request #2216 · google/bigframes

Conversation

@shuoweil

@shuoweil shuoweil commented Oct 31, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

This commit addresses issue where creating empty DataFrames with nested JSON columns would fail due to PyArrow's inability to create empty arrays with db_dtypes.JSONArrowType (Apache Arrow issue #45262).

Changes:

  • First tries to create an empty Arrow table directly from the schema
  • If that fails with pa.ArrowNotImplementedError, falls back to using storage types (use_storage_types=True)
  • Converts the Arrow table to pandas, which properly preserves dtypes

This workaround is specifically needed for the anywidget backend which uses to_pandas_batches()

Fixes #<456577463> 🦕

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
shuoweil added 2 commits October 30, 2025 21:46
@shuoweil
Correctly display DataFrames with JSON columns in anywidget
8c34512
@shuoweil
Improve JSON type handling for to_gbq and to_pandas_batches
05e9b69
@shuoweil
shuoweil requested review from chelsea-lin and tswast October 31, 2025 00:02
@shuoweil shuoweil self-assigned this Oct 31, 2025
@shuoweil
shuoweil requested review from a team October 31, 2025 00:02
@review-notebook-app

review-notebook-app Bot commented Oct 31, 2025

Copy link
Copy Markdown

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@product-auto-label product-auto-label Bot added size: l Pull request size is large. api: bigquery Issues related to the googleapis/python-bigquery-dataframes API. labels Oct 31, 2025
@shuoweil
Revert "Correctly display DataFrames with JSON columns in anywidget"
aa04bac
This reverts commit 8c34512.
@product-auto-label product-auto-label Bot added size: m Pull request size is medium. and removed size: l Pull request size is large. labels Oct 31, 2025
@shuoweil
Remove unnecessary comment
592e43b
tswast
tswast reviewed Oct 31, 2025
View reviewed changes

@tswast tswast left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Look good to me once typing and presubmit failures are addressed.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread bigframes/core/blocks.py Outdated
Comment on lines +725 to +748
for col in itertools.chain(self.value_columns, self.index_columns):
dtype = self.expr.get_column_type(col)
if bigframes.dtypes.contains_db_dtypes_json_dtype(dtype):
# Due to a limitation in Apache Arrow (#45262), JSON columns are not
# natively supported by the to_pandas_batches() method, which is
# used by the anywidget backend.
# Workaround for https://github.com/googleapis/python-bigquery-dataframes/issues/1273
# PyArrow doesn't support creating an empty array with db_dtypes.JSONArrowType,
# especially when nested.
# Create with string type and then cast.

# MyPy doesn't automatically narrow the type of 'dtype' here,
# so we add an explicit check.
if isinstance(dtype, pd.ArrowDtype):
safe_pa_type = bigframes.dtypes._replace_json_arrow_with_string(
dtype.pyarrow_dtype
)
safe_dtype = pd.ArrowDtype(safe_pa_type)
series_map[col] = pd.Series([], dtype=safe_dtype).astype(dtype)
else:
# This branch should ideally not be reached if
# contains_db_dtypes_json_dtype is accurate,
# but it's here for MyPy's sake.
series_map[col] = pd.Series([], dtype=dtype)

@tswast tswast Oct 31, 2025

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chelsea-lin I assume we have similar code that does this, right? Maybe there's something that could be reused here?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@chelsea-lin chelsea-lin Oct 31, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, we have something similar in the loader component but they're slightly different.

https://github.com/googleapis/python-bigquery-dataframes/blob/316ba9f557d792117d5a7845d7567498f78dd513/bigframes/session/loader.py#L1319

Also, I agree that we can simply logic a little bit, for example:

dtype = pd.ArrowDtype(pa.list_(pa.struct([("key", db_dtypes.JSONArrowType())])))
try:
    s = pd.Series([], dtype=dtype)
except pa.ArrowNotImplementedError as e:
    s = pd.Series([], dtype=pd.ArrowDtype(_replace_json_arrow_with_string(dtype.pyarrow_dtype))).astype(dtype)

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

shuoweil reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@shuoweil shuoweil Oct 31, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The logic has been simplified

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@chelsea-lin chelsea-lin Nov 3, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The new logic looks even better!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@tswast tswast changed the title fix: Pyarrow limitation with empty nested JSON arrays in to_pandas_batches() fix: support results with STRUCT and ARRAY columns containing JSON subfields in to_pandas_batches() Oct 31, 2025
@tswast

tswast commented Oct 31, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Collaborator

Nit: I renamed the PR to be a little more user-oriented. Users don't care as much about the internal limitations. What changed from their perspective is that they can read STRUCT<JSON> and ARRAY<JSON> now.

shuoweil reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

chelsea-lin
chelsea-lin reviewed Oct 31, 2025
View reviewed changes
Comment thread bigframes/dtypes.py Outdated
return contains_db_dtypes_json_arrow_type(dtype.pyarrow_dtype)


def _replace_json_arrow_with_string(pa_type: pa.DataType) -> pa.DataType:

@chelsea-lin chelsea-lin Oct 31, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function may be similar as the following two methods. Can you help to remove the one in the loader.py?

https://github.com/googleapis/python-bigquery-dataframes/blob/316ba9f557d792117d5a7845d7567498f78dd513/bigframes/session/loader.py#L1303

https://github.com/googleapis/python-bigquery-dataframes/blob/316ba9f557d792117d5a7845d7567498f78dd513/bigframes/dtypes.py#L935

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

shuoweil reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

@shuoweil shuoweil Oct 31, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since I removed this function, this code refactor is no longer relevant to this PR. I will start a new PR (#2221) for this code refactor.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

chelsea-lin reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
shuoweil added 4 commits October 31, 2025 17:58
@shuoweil
Merge branch 'main' into shuowei-json-empty-dataframe
3b86941
@shuoweil
code refactor
5955bfe
@shuoweil
testcase update
d07ba7e
@shuoweil
Merge branch 'main' into shuowei-json-empty-dataframe
9b1fb93
@shuoweil
shuoweil requested review from chelsea-lin and tswast October 31, 2025 20:32
shuoweil added 6 commits October 31, 2025 21:27
@shuoweil
Fix testcase
d7455a6
@shuoweil
Merge branch 'main' into shuowei-json-empty-dataframe
066b8d6
@shuoweil
function call updated in
12e2a63
  bigframes/core/blocks.py, unused function removed from bigframes/dtypes.py
@shuoweil
revert the code refactor in loader.py, I will use a seperate pr for t…
393a2f9
…his refactor
@shuoweil
replace the manual
2ff0108
  construction of the empty DataFrame with the more robust try...except block
  that leverages to_pyarrow and empty_table
@shuoweil
fix testcase
512e3a1
shuoweil added 3 commits November 1, 2025 00:51
@shuoweil
existing arrow_to_pandas() helper that properly handles dtype conver…
5f5881b
…sion
@shuoweil
testcase update
3119771
@shuoweil
refactor testcase
be1dea4
@shuoweil shuoweil added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 1, 2025
@yoshi-kokoro yoshi-kokoro removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 1, 2025
@shuoweil shuoweil added the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 3, 2025
@bigframes-bot bigframes-bot removed the kokoro:force-run Add this label to force Kokoro to re-run the tests. label Nov 3, 2025
chelsea-lin
chelsea-lin suggested changes Nov 3, 2025
View reviewed changes
Comment thread bigframes/core/blocks.py Outdated
try:
empty_arrow_table = self.expr.schema.to_pyarrow().empty_table()
except pa.ArrowNotImplementedError:
# Bug with some pyarrow versions, empty_table only supports base storage types, not extension types.

@chelsea-lin chelsea-lin Nov 3, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can you please add the bug id in the docs.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

shuoweil reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
Comment thread bigframes/core/blocks.py Outdated
Comment on lines +725 to +748
for col in itertools.chain(self.value_columns, self.index_columns):
dtype = self.expr.get_column_type(col)
if bigframes.dtypes.contains_db_dtypes_json_dtype(dtype):
# Due to a limitation in Apache Arrow (#45262), JSON columns are not
# natively supported by the to_pandas_batches() method, which is
# used by the anywidget backend.
# Workaround for https://github.com/googleapis/python-bigquery-dataframes/issues/1273
# PyArrow doesn't support creating an empty array with db_dtypes.JSONArrowType,
# especially when nested.
# Create with string type and then cast.

# MyPy doesn't automatically narrow the type of 'dtype' here,
# so we add an explicit check.
if isinstance(dtype, pd.ArrowDtype):
safe_pa_type = bigframes.dtypes._replace_json_arrow_with_string(
dtype.pyarrow_dtype
)
safe_dtype = pd.ArrowDtype(safe_pa_type)
series_map[col] = pd.Series([], dtype=safe_dtype).astype(dtype)
else:
# This branch should ideally not be reached if
# contains_db_dtypes_json_dtype is accurate,
# but it's here for MyPy's sake.
series_map[col] = pd.Series([], dtype=dtype)

@chelsea-lin chelsea-lin Nov 3, 2025

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! The new logic looks even better!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
shuoweil added 2 commits November 3, 2025 21:30
@shuoweil
Add pyarrow id to comments
5ed4293
@shuoweil
Merge branch 'main' into shuowei-json-empty-dataframe
0d50c6d
@shuoweil
shuoweil requested a review from chelsea-lin November 3, 2025 23:58
@shuoweil
shuoweil enabled auto-merge (squash) November 3, 2025 23:59
chelsea-lin
chelsea-lin approved these changes Nov 4, 2025
View reviewed changes
@shuoweil
shuoweil disabled auto-merge November 4, 2025 00:10
@shuoweil
shuoweil merged commit 3d8b17f into main Nov 4, 2025
@shuoweil
shuoweil deleted the shuowei-json-empty-dataframe branch November 4, 2025 01:27
@release-please release-please Bot mentioned this pull request Nov 4, 2025
chore(main): release 2.29.0 #2225
Closed
@shuoweil shuoweil mentioned this pull request Nov 4, 2025
docs: update notebook for JSON subfields support in to_pandas_batches() #2138
Merged
@parthea parthea mentioned this pull request Nov 7, 2025
chore: librarian release pull request: 20251107T184449Z #2241
Closed
@release-please release-please Bot mentioned this pull request Nov 7, 2025
chore(main): release 2.29.0 #2242
Merged
sycai pushed a commit that referenced this pull request Nov 10, 2025
@release-please
chore(main): release 2.29.0 (#2242)
44e9869
🤖 I have created a release *beep* *boop*
---


##
[2.29.0](v2.28.0...v2.29.0)
(2025-11-10)


### Features

* Add bigframes.bigquery.st_regionstats to join raster data from Earth
Engine
([#2228](#2228))
([10ec52f](10ec52f))
* Add DataFrame.resample and Series.resample
([#2213](#2213))
([c9ca02c](c9ca02c))
* SQL Cell no longer escapes formatted string values
([#2245](#2245))
([d2d38f9](d2d38f9))
* Support left_index and right_index for merge
([#2220](#2220))
([da9ba26](da9ba26))


### Bug Fixes

* Correctly iterate over null struct values in ManagedArrowTable
([#2209](#2209))
([12e04d5](12e04d5))
* Simplify UnsupportedTypeError message
([#2212](#2212))
([6c9a18d](6c9a18d))
* Support results with STRUCT and ARRAY columns containing JSON
subfields in `to_pandas_batches()`
([#2216](#2216))
([3d8b17f](3d8b17f))


### Documentation

* Switch API reference docs to pydata theme
([#2237](#2237))
([9b86dcf](9b86dcf))
* Update notebook for JSON subfields support in to_pandas_batches()
([#2138](#2138))
([5663d2a](5663d2a))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
@gkevinzheng gkevinzheng mentioned this pull request Nov 13, 2025
chore: librarian release pull request: 20251113T215818Z #2265
Closed
parthea pushed a commit to googleapis/google-cloud-python that referenced this pull request Apr 1, 2026
@release-please
chore(main): release 2.29.0 (#2242)
a69afc4
🤖 I have created a release *beep* *boop*
---


##
[2.29.0](google/bigframes@v2.28.0...v2.29.0)
(2025-11-10)


### Features

* Add bigframes.bigquery.st_regionstats to join raster data from Earth
Engine
([#2228](google/bigframes#2228))
([10ec52f](google/bigframes@10ec52f))
* Add DataFrame.resample and Series.resample
([#2213](google/bigframes#2213))
([c9ca02c](google/bigframes@c9ca02c))
* SQL Cell no longer escapes formatted string values
([#2245](google/bigframes#2245))
([d2d38f9](google/bigframes@d2d38f9))
* Support left_index and right_index for merge
([#2220](google/bigframes#2220))
([da9ba26](google/bigframes@da9ba26))


### Bug Fixes

* Correctly iterate over null struct values in ManagedArrowTable
([#2209](google/bigframes#2209))
([12e04d5](google/bigframes@12e04d5))
* Simplify UnsupportedTypeError message
([#2212](google/bigframes#2212))
([6c9a18d](google/bigframes@6c9a18d))
* Support results with STRUCT and ARRAY columns containing JSON
subfields in `to_pandas_batches()`
([#2216](google/bigframes#2216))
([3d8b17f](google/bigframes@3d8b17f))


### Documentation

* Switch API reference docs to pydata theme
([#2237](google/bigframes#2237))
([9b86dcf](google/bigframes@9b86dcf))
* Update notebook for JSON subfields support in to_pandas_batches()
([#2138](google/bigframes#2138))
([5663d2a](google/bigframes@5663d2a))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: release-please[bot] <55107282+release-please[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@tswast tswast Awaiting requested review from tswast
1 more reviewer
@chelsea-lin chelsea-lin chelsea-lin approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@shuoweil shuoweil

Labels

api: bigquery Issues related to the googleapis/python-bigquery-dataframes API. size: m Pull request size is medium.

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

5 participants

@shuoweil @tswast @chelsea-lin @yoshi-kokoro @bigframes-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.