Skip to content

Commit 132227e

Browse files
committed
Fix: Handle empty index in unpivot with identity mapping
1 parent 7ad3a04 commit 132227e

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

bigframes/core/blocks.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3435,7 +3435,15 @@ def unpivot(
34353435
# Last column is offsets
34363436
if not labels_array.column_ids:
34373437
# Handle empty column_ids case for multimodal DataFrames
3438-
return array_value, (tuple(), tuple(), tuple(passthrough_columns))
3438+
# When no index columns exist, return original array_value with identity mappings
3439+
value_cols = [
3440+
col for col in array_value.column_ids if col not in passthrough_columns
3441+
]
3442+
return array_value, (
3443+
tuple(),
3444+
tuple(value_cols),
3445+
tuple(passthrough_columns),
3446+
)
34393447
index_col_ids = [labels_mapping[col] for col in labels_array.column_ids[:-1]]
34403448
explode_offsets_id = labels_mapping[labels_array.column_ids[-1]]
34413449

@@ -3462,7 +3470,7 @@ def unpivot(
34623470
joined_array, unpivot_col_ids = joined_array.compute_values(unpivot_exprs)
34633471

34643472
return joined_array.select_columns(
3465-
[*index_col_ids, *unpivot_col_ids, *new_passthrough_cols]
3473+
[*index_col_ids, *unpivot_col_ids, *new_passthrough_cols], allow_renames=True
34663474
), (tuple(index_col_ids), tuple(unpivot_col_ids), tuple(new_passthrough_cols))
34673475

34683476

tests/unit/core/test_blocks_unpivot.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,20 +58,21 @@ def test_unpivot_with_empty_row_labels(mock_session):
5858
import pyarrow as pa
5959

6060
# Create a dummy ArrayValue
61-
df = pd.DataFrame({"a": [1, 2, 3]})
61+
df = pd.DataFrame({"a": [1, 2, 3], "b": [4, 5, 6]})
6262
pa_table = pa.Table.from_pandas(df)
6363
array_value = blocks.core.ArrayValue.from_pyarrow(pa_table, session=mock_session)
6464

6565
# Call unpivot with an empty pd.Index
66-
unpivot_result, (index_cols, unpivot_cols, passthrough_cols) = blocks.unpivot(
66+
unpivot_result, (index_cols, value_cols, passthrough_cols) = blocks.unpivot(
6767
array_value,
6868
row_labels=pd.Index([]),
6969
unpivot_columns=[("a",)],
70+
passthrough_columns=["b"],
7071
)
7172

7273
# The expected behavior is that the unpivot operation does nothing and returns
73-
# the original array_value and empty column tuples.
74+
# the original array_value and identity mappings.
7475
assert unpivot_result is array_value
7576
assert index_cols == tuple()
76-
assert unpivot_cols == tuple()
77-
assert passthrough_cols == tuple()
77+
assert value_cols == ("a",)
78+
assert passthrough_cols == ("b",)

0 commit comments

Comments
 (0)