Skip to content

Commit 169b9f4

Browse files
committed
Fix: Handle single-column DataFrame dtypes in TableWidget
1 parent a6195a4 commit 169b9f4

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

bigframes/display/anywidget.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,14 @@ def __init__(self, dataframe: bigframes.dataframe.DataFrame):
110110
# TODO(b/469861913): Nested columns from structs (e.g., 'struct_col.name') are not currently sortable.
111111
# TODO(b/463754889): Support non-string column labels for sorting.
112112
if all(isinstance(col, str) for col in dataframe.columns):
113+
# TODO(b/462525985): dataframe.dtypes should always be a Series.
114+
items = (
115+
dataframe.dtypes.items()
116+
if isinstance(dataframe.dtypes, pd.Series)
117+
else [(dataframe.columns[0], dataframe.dtypes)]
118+
)
113119
self.orderable_columns = [
114-
str(col_name)
115-
for col_name, dtype in dataframe.dtypes.items()
116-
if dtypes.is_orderable(dtype)
120+
str(col_name) for col_name, dtype in items if dtypes.is_orderable(dtype)
117121
]
118122
else:
119123
self.orderable_columns = []

tests/unit/display/test_anywidget.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,32 @@ def test_page_size_change_resets_sort(mock_df):
153153

154154
# to_pandas_batches called again (reset)
155155
assert mock_df.to_pandas_batches.call_count >= 2
156+
157+
158+
def test_table_widget_single_column_dataframe():
159+
"""Test that TableWidget can be created with a single-column DataFrame."""
160+
from bigframes.display.anywidget import TableWidget
161+
import bigframes.dtypes
162+
163+
mock_df = mock.create_autospec(bigframes.dataframe.DataFrame, instance=True)
164+
mock_df.columns = ["image"]
165+
# This is the key part: simulate single-column DataFrame's dtypes property
166+
mock_df.dtypes = bigframes.dtypes.STRING_DTYPE # Using string as a stand-in
167+
168+
mock_block = mock.Mock()
169+
mock_block.has_index = False
170+
mock_df._block = mock_block
171+
172+
# We mock _initial_load to avoid complex setup
173+
with mock.patch.object(TableWidget, "_initial_load"):
174+
with bigframes.option_context(
175+
"display.repr_mode", "anywidget", "display.max_rows", 10
176+
):
177+
try:
178+
widget = TableWidget(mock_df)
179+
# Check if 'image' column is considered orderable (it is a string dtype)
180+
assert "image" in widget.orderable_columns
181+
except IndexError:
182+
pytest.fail(
183+
"Creating TableWidget with single-column DataFrame raised IndexError"
184+
)

0 commit comments

Comments
 (0)