Skip to content

Commit a04c92b

Browse files
committed
disable sorting for integer or multiindex
1 parent 25a6ade commit a04c92b

File tree

3 files changed

+67
-15
lines changed

3 files changed

+67
-15
lines changed

bigframes/display/anywidget.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,11 +108,15 @@ def __init__(self, dataframe: bigframes.dataframe.DataFrame):
108108
# set traitlets properties that trigger observers
109109
# TODO(b/462525985): Investigate and improve TableWidget UX for DataFrames with a large number of columns.
110110
self.page_size = initial_page_size
111-
self.orderable_columns = [
112-
str(col_name)
113-
for col_name, dtype in dataframe.dtypes.items()
114-
if dtypes.is_orderable(dtype)
115-
]
111+
# TODO(b/463754889): Support non-string column labels for sorting.
112+
if all(isinstance(col, str) for col in dataframe.columns):
113+
self.orderable_columns = [
114+
col_name
115+
for col_name, dtype in dataframe.dtypes.items()
116+
if dtypes.is_orderable(dtype)
117+
]
118+
else:
119+
self.orderable_columns = []
116120

117121
# obtain the row counts
118122
# TODO(b/428238610): Start iterating over the result of `to_pandas_batches()`
@@ -262,6 +266,7 @@ def _set_table_html(self) -> None:
262266
# Apply sorting if a column is selected
263267
df_to_display = self._dataframe
264268
if self.sort_column:
269+
# TODO(b/463715504): Support sorting by index columns.
265270
df_to_display = df_to_display.sort_values(
266271
by=self.sort_column, ascending=self.sort_ascending
267272
)

notebooks/dataframes/anywidget_mode.ipynb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@
157157
{
158158
"data": {
159159
"application/vnd.jupyter.widget-view+json": {
160-
"model_id": "f0f1cc04e1564a6ab4ba39935aebd11a",
160+
"model_id": "3ad3ce907f4d43de8c3c15f0d1cdebd6",
161161
"version_major": 2,
162162
"version_minor": 1
163163
},
@@ -255,7 +255,7 @@
255255
{
256256
"data": {
257257
"application/vnd.jupyter.widget-view+json": {
258-
"model_id": "bed78e341154486bb49841ce12a0af93",
258+
"model_id": "684becdc45de4a15826672c0a7e89930",
259259
"version_major": 2,
260260
"version_minor": 1
261261
},
@@ -369,7 +369,7 @@
369369
{
370370
"data": {
371371
"application/vnd.jupyter.widget-view+json": {
372-
"model_id": "357c732c110e44fa9efe3e512da61638",
372+
"model_id": "5cccf9eb2a674f9f8a1bdb165921d5f9",
373373
"version_major": 2,
374374
"version_minor": 1
375375
},
@@ -409,7 +409,7 @@
409409
"data": {
410410
"text/html": [
411411
"✅ Completed. \n",
412-
" Query processed 85.9 kB in 14 seconds of slot time.\n",
412+
" Query processed 85.9 kB in 13 seconds of slot time.\n",
413413
" "
414414
],
415415
"text/plain": [
@@ -456,7 +456,7 @@
456456
{
457457
"data": {
458458
"application/vnd.jupyter.widget-view+json": {
459-
"model_id": "af87c96ce359412a99b5eabbc1b9d137",
459+
"model_id": "06e704906f254de194e5e3ea1ef2bdbb",
460460
"version_major": 2,
461461
"version_minor": 1
462462
},

tests/system/small/test_anywidget.py

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -721,11 +721,6 @@ def test_widget_with_unknown_row_count_empty_dataframe(
721721
assert widget.page == 0
722722

723723

724-
# TODO(shuowei): Add tests for custom index and multiindex
725-
# This may not be necessary for the SQL Cell use case but should be
726-
# considered for completeness.
727-
728-
729724
def test_widget_sort_should_sort_ascending_on_first_click(
730725
table_widget, paginated_pandas_df: pd.DataFrame
731726
):
@@ -817,3 +812,55 @@ def test_widget_sort_should_reset_on_page_size_change(
817812
html = table_widget.table_html
818813

819814
_assert_html_matches_pandas_slice(html, expected_slice, paginated_pandas_df)
815+
816+
817+
@pytest.fixture(scope="module")
818+
def integer_column_df(session):
819+
"""Create a DataFrame with integer column labels."""
820+
pandas_df = pd.DataFrame([[0, 1], [2, 3]], columns=pd.Index([1, 2]))
821+
return session.read_pandas(pandas_df)
822+
823+
824+
@pytest.fixture(scope="module")
825+
def multiindex_column_df(session):
826+
"""Create a DataFrame with MultiIndex column labels."""
827+
pandas_df = pd.DataFrame(
828+
{
829+
"foo": ["one", "one", "one", "two", "two", "two"],
830+
"bar": ["A", "B", "C", "A", "B", "C"],
831+
"baz": [1, 2, 3, 4, 5, 6],
832+
"zoo": ["x", "y", "z", "q", "w", "t"],
833+
}
834+
)
835+
df = session.read_pandas(pandas_df)
836+
# The session is attached to `df` through the constructor.
837+
# We can pass it to the pivoted DataFrame.
838+
pdf = df.pivot(index="foo", columns="bar", values=["baz", "zoo"])
839+
return pdf
840+
841+
842+
def test_table_widget_integer_columns_disables_sorting(integer_column_df):
843+
"""
844+
Given a DataFrame with integer column labels, the widget should
845+
disable sorting.
846+
"""
847+
from bigframes.display import TableWidget
848+
849+
widget = TableWidget(integer_column_df)
850+
assert widget.orderable_columns == []
851+
852+
853+
def test_table_widget_multiindex_columns_disables_sorting(multiindex_column_df):
854+
"""
855+
Given a DataFrame with a MultiIndex for columns, the widget should
856+
disable sorting.
857+
"""
858+
from bigframes.display import TableWidget
859+
860+
widget = TableWidget(multiindex_column_df)
861+
assert widget.orderable_columns == []
862+
863+
864+
# TODO(shuowei): Add tests for custom index and multiindex
865+
# This may not be necessary for the SQL Cell use case but should be
866+
# considered for completeness.

0 commit comments

Comments
 (0)