Skip to content

Commit 153e1d2

Browse files
committed
add todo and revert change
1 parent 8601e52 commit 153e1d2

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

bigframes/dataframe.py

Lines changed: 29 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import re
2424
import sys
2525
import textwrap
26+
import traceback
2627
import typing
2728
from typing import (
2829
Any,
@@ -782,9 +783,9 @@ def __repr__(self) -> str:
782783

783784
opts = bigframes.options.display
784785
max_results = opts.max_rows
785-
786-
# Only deferred mode shows dry run
787-
if opts.repr_mode in ("deferred"):
786+
# anywdiget mode uses the same display logic as the "deferred" mode
787+
# for faster execution
788+
if opts.repr_mode in ("deferred", "anywidget"):
788789
return formatter.repr_query_job(self._compute_dry_run())
789790

790791
# TODO(swast): pass max_columns and get the true column count back. Maybe
@@ -850,27 +851,27 @@ def _repr_html_(self) -> str:
850851

851852
if opts.repr_mode == "anywidget":
852853
try:
853-
import anywidget # noqa: F401
854854
from IPython.display import display as ipython_display
855-
import traitlets # noqa: F401
856855

857856
from bigframes import display
858-
except ImportError:
857+
858+
# Always create a new widget instance for each display call
859+
# This ensures that each cell gets its own widget and prevents
860+
# unintended sharing between cells
861+
widget = display.TableWidget(df.copy())
862+
863+
ipython_display(widget)
864+
return "" # Return empty string since we used display()
865+
866+
except (AttributeError, ValueError, ImportError):
867+
# Fallback if anywidget is not available
859868
warnings.warn(
860-
"anywidget or its dependencies are not installed. "
869+
"Anywidget mode is not available. "
861870
"Please `pip install anywidget traitlets` or `pip install 'bigframes[anywidget]'` to use interactive tables. "
862-
"Falling back to deferred mode."
871+
f"Falling back to deferred mode. Error: {traceback.format_exc()}"
863872
)
864873
return formatter.repr_query_job(self._compute_dry_run())
865874

866-
# Always create a new widget instance for each display call
867-
# This ensures that each cell gets its own widget and prevents
868-
# unintended sharing between cells
869-
widget = display.TableWidget(df.copy())
870-
871-
ipython_display(widget)
872-
return "" # Return empty string since we used display()
873-
874875
# Continue with regular HTML rendering for non-anywidget modes
875876
# TODO(swast): pass max_columns and get the true column count back. Maybe
876877
# get 1 more column than we have requested so that pandas can add the
@@ -2563,33 +2564,25 @@ def sort_index(
25632564
) -> None:
25642565
...
25652566

2567+
@validations.requires_index
25662568
def sort_index(
25672569
self,
25682570
*,
2569-
axis: Union[int, str] = 0,
25702571
ascending: bool = True,
25712572
inplace: bool = False,
25722573
na_position: Literal["first", "last"] = "last",
25732574
) -> Optional[DataFrame]:
2574-
if utils.get_axis_number(axis) == 0:
2575-
if na_position not in ["first", "last"]:
2576-
raise ValueError("Param na_position must be one of 'first' or 'last'")
2577-
na_last = na_position == "last"
2578-
index_columns = self._block.index_columns
2579-
ordering = [
2580-
order.ascending_over(column, na_last)
2581-
if ascending
2582-
else order.descending_over(column, na_last)
2583-
for column in index_columns
2584-
]
2585-
block = self._block.order_by(ordering)
2586-
else: # axis=1
2587-
_, indexer = self.columns.sort_values(
2588-
return_indexer=True, ascending=ascending, na_position=na_position # type: ignore
2589-
)
2590-
block = self._block.select_columns(
2591-
[self._block.value_columns[i] for i in indexer]
2592-
)
2575+
if na_position not in ["first", "last"]:
2576+
raise ValueError("Param na_position must be one of 'first' or 'last'")
2577+
na_last = na_position == "last"
2578+
index_columns = self._block.index_columns
2579+
ordering = [
2580+
order.ascending_over(column, na_last)
2581+
if ascending
2582+
else order.descending_over(column, na_last)
2583+
for column in index_columns
2584+
]
2585+
block = self._block.order_by(ordering)
25932586
if inplace:
25942587
self._set_block(block)
25952588
return None

bigframes/display/anywidget.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,9 @@ def _reset_batches_for_new_page_size(self):
191191

192192
def _set_table_html(self):
193193
"""Sets the current html data based on the current page and page size."""
194+
# TODO (shuowei): BigFrames Series with db_dtypes.JSONArrowType column
195+
# fails to convert to pandas DataFrame in anywidget environment due to
196+
# missing handling in to_pandas_batches(). b/453561268
194197
# For empty dataframe, render empty table with headers.
195198
if self.row_count == 0:
196199
page_data = self._cached_data

0 commit comments

Comments
 (0)