Skip to content

Commit bbb4096

Browse files
committed
Fix: Handle empty index in unpivot operation
1 parent e1e1141 commit bbb4096

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

bigframes/core/blocks.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3471,6 +3471,27 @@ def _pd_index_to_array_value(
34713471
Create an ArrayValue from a list of label tuples.
34723472
The last column will be row offsets.
34733473
"""
3474+
if index.empty:
3475+
id_gen = bigframes.core.identifiers.standard_id_strings()
3476+
col_ids = [next(id_gen) for _ in range(index.nlevels + 1)]
3477+
3478+
data_dict = {}
3479+
if isinstance(index, pd.MultiIndex):
3480+
dtypes = index.dtypes.values.tolist()
3481+
else:
3482+
dtypes = [index.dtype]
3483+
3484+
for col_id, dtype in zip(col_ids[:-1], dtypes):
3485+
try:
3486+
bf_dtype = bigframes.dtypes.bigframes_type(dtype)
3487+
pa_type = bigframes.dtypes.bigframes_dtype_to_arrow_dtype(bf_dtype)
3488+
except TypeError:
3489+
pa_type = pa.string()
3490+
data_dict[col_id] = pa.array([], type=pa_type)
3491+
3492+
data_dict[col_ids[-1]] = pa.array([], type=pa.int64())
3493+
table = pa.Table.from_pydict(data_dict)
3494+
return core.ArrayValue.from_pyarrow(table, session=session)
34743495
rows = []
34753496
labels_as_tuples = utils.index_as_tuples(index)
34763497
for row_offset in range(len(index)):
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
# Copyright 2025 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
from unittest import mock
16+
17+
import pandas as pd
18+
import pytest
19+
20+
from bigframes.core import blocks
21+
22+
23+
@pytest.fixture
24+
def mock_session():
25+
session = mock.MagicMock()
26+
session.bqclient = None
27+
return session
28+
29+
30+
def test_pd_index_to_array_value_with_empty_index_creates_columns(mock_session):
31+
"""
32+
Tests that `_pd_index_to_array_value` correctly handles an empty pandas Index by creating
33+
an ArrayValue with the expected columns (index column + offset column).
34+
This prevents crashes in `unpivot` which expects these columns to exist.
35+
"""
36+
empty_index = pd.Index([], name="test")
37+
38+
array_val = blocks._pd_index_to_array_value(mock_session, empty_index)
39+
40+
# Should be 2: one for index, one for offset
41+
assert len(array_val.column_ids) == 2
42+
43+
44+
def test_pd_index_to_array_value_with_empty_multiindex_creates_columns(mock_session):
45+
"""
46+
Tests that `_pd_index_to_array_value` correctly handles an empty pandas MultiIndex by creating
47+
an ArrayValue with the expected columns (one for each level + offset column).
48+
"""
49+
empty_index = pd.MultiIndex.from_arrays([[], []], names=["a", "b"])
50+
51+
array_val = blocks._pd_index_to_array_value(mock_session, empty_index)
52+
53+
# Should have 3 columns: a, b, offset
54+
assert len(array_val.column_ids) == 3

0 commit comments

Comments
 (0)