Skip to content

fix: support melting empty DataFrames without crashing#2509

Open
shuoweil wants to merge 9 commits intomainfrom
shuowei-fix-unpivot-empty
Open

fix: support melting empty DataFrames without crashing#2509
shuoweil wants to merge 9 commits intomainfrom
shuowei-fix-unpivot-empty

Conversation

@shuoweil
Copy link
Contributor

@shuoweil shuoweil commented Mar 12, 2026

Allows alignment melts over zero-row offset layouts

Fixes #<452681068> 🦕

@shuoweil shuoweil self-assigned this Mar 12, 2026
@shuoweil shuoweil requested review from a team as code owners March 12, 2026 02:42
@shuoweil shuoweil requested a review from chelsea-lin March 12, 2026 02:42
@product-auto-label product-auto-label bot added size: m Pull request size is medium. api: bigquery Issues related to the googleapis/python-bigquery-dataframes API. labels Mar 12, 2026
@shuoweil shuoweil changed the title Shuowei fix unpivot empty fix: support melting empty DataFrames without crashing Mar 12, 2026
assert table.labels
assert table.labels["test"] == "labels"


Copy link
Contributor

Choose a reason for hiding this comment

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

nit: move test_dataframe_melt_multiindex to tests/system/small/test_multiindex.py file, which has tests related to multi index.

rows.append(row)

return core.ArrayValue.from_pyarrow(pa.Table.from_pylist(rows), session=session)
import pyarrow as pa
Copy link
Contributor

Choose a reason for hiding this comment

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

IIUC, this import can be removed because the header of this file has the pyarrow importing?

import pyarrow as pa

if not rows:
from bigframes.dtypes import bigframes_dtype_to_arrow_dtype
Copy link
Contributor

Choose a reason for hiding this comment

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

we have bigframes.dtypes import in the head of this file.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This testcase has been cleaned up.

assert table.labels["test"] == "labels"


def test_dataframe_melt_multiindex(session):
Copy link
Contributor

Choose a reason for hiding this comment

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

It looks like I cannot reproduce the issue on the main branch. See screenshot/AXRSuFmXPTt5gzj

Copy link
Contributor Author

@shuoweil shuoweil Mar 19, 2026

Choose a reason for hiding this comment

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

Good catch! test_dataframe_melt_multiindex didn't catch the syntax error because the DataFrame wasn't actually empty. The core issue only happens when we try to unpivot an empty selection, causing an untyped NULL compilation.

To properly cover this edge case, I added test_count_empty_multiindex_columns in test_multiindex.py. Note that the test explicitly asserts the generated .to_frame().sql locally. If we run .to_pandas() directly, it trips over an unrelated Ibis compiler bug with empty [STRUCT()] layouts on the BigQuery backend, which I am fixing in a separate upcoming PR. This test safely completely isolates the unpivot fix in this branch..

@shuoweil shuoweil force-pushed the shuowei-fix-unpivot-empty branch from 32ce90c to f03758d Compare March 19, 2026 21:48
@shuoweil shuoweil requested a review from chelsea-lin March 19, 2026 21:52
@shuoweil shuoweil force-pushed the shuowei-fix-unpivot-empty branch from 77dff89 to 2d1c6d4 Compare March 20, 2026 04:38
@shuoweil shuoweil force-pushed the shuowei-fix-unpivot-empty branch from 2d1c6d4 to f8241d7 Compare March 20, 2026 18:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants