Skip to content

Commit c386802

Browse files
committed
change return type
1 parent 90c1d3a commit c386802

File tree

4 files changed

+13
-34
lines changed

4 files changed

+13
-34
lines changed

bigframes/core/blocks.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999

100100

101101
@dataclasses.dataclass
102-
class PandasBatches(Iterator[pd.DataFrame]):
102+
class PandasBatches:
103103
"""Interface for mutable objects with state represented by a block value object."""
104104

105105
def __init__(
@@ -124,6 +124,9 @@ def total_bytes_processed(self) -> Optional[int]:
124124
def __next__(self) -> pd.DataFrame:
125125
return next(self._dataframes)
126126

127+
def __iter__(self) -> Iterator[pd.DataFrame]:
128+
return self
129+
127130

128131
@dataclasses.dataclass()
129132
class MaterializationOptions:
@@ -693,7 +696,7 @@ def to_pandas_batches(
693696
page_size: Optional[int] = None,
694697
max_results: Optional[int] = None,
695698
allow_large_results: Optional[bool] = None,
696-
) -> Iterator[pd.DataFrame]:
699+
) -> PandasBatches:
697700
"""Download results one message at a time.
698701
699702
page_size and max_results determine the size and number of batches,

bigframes/dataframe.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,7 +1892,7 @@ def to_pandas_batches(
18921892
max_results: Optional[int] = None,
18931893
*,
18941894
allow_large_results: Optional[bool] = None,
1895-
) -> Iterable[pandas.DataFrame]:
1895+
) -> blocks.PandasBatches:
18961896
"""Stream DataFrame results to an iterable of pandas DataFrame.
18971897
18981898
page_size and max_results determine the size and number of batches,
@@ -1937,7 +1937,7 @@ def to_pandas_batches(
19371937
over the default size limit of 10 GB.
19381938
19391939
Returns:
1940-
Iterable[pandas.DataFrame]:
1940+
bigframes.core.blocks.PandasBatches:
19411941
An iterable of smaller dataframes which combine to
19421942
form the original dataframe. Results stream from bigquery,
19431943
see https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.table.RowIterator#google_cloud_bigquery_table_RowIterator_to_arrow_iterable

bigframes/display/anywidget.py

Lines changed: 4 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,13 @@
1717
from importlib import resources
1818
import functools
1919
import math
20-
from typing import Any, cast, Dict, Iterator, List, Optional, Type
20+
from typing import Any, Dict, Iterator, List, Optional, Type
2121
import uuid
2222

2323
import pandas as pd
2424

2525
import bigframes
26-
import bigframes.core.blocks
2726
import bigframes.display.html
28-
import bigframes.session.execution_spec
2927

3028
# anywidget and traitlets are optional dependencies. We don't want the import of
3129
# this module to fail if they aren't installed, though. Instead, we try to
@@ -90,21 +88,11 @@ def __init__(self, dataframe: bigframes.dataframe.DataFrame):
9088
# Respect display options for initial page size
9189
self.page_size = bigframes.options.display.max_rows
9290

93-
# Force execution with explicit destination to get total_rows metadata
94-
execute_result = dataframe._block.session._executor.execute(
95-
dataframe._block.expr,
96-
execution_spec=bigframes.session.execution_spec.ExecutionSpec(
97-
ordered=True, promise_under_10gb=False
98-
),
99-
)
10091
# The query issued by `to_pandas_batches()` already contains
10192
# metadata about how many results there were. Use that to avoid
10293
# doing an extra COUNT(*) query that `len(...)` would do.
103-
self.row_count = execute_result.total_rows or 0
104-
self._batches = cast(
105-
bigframes.core.blocks.PandasBatches,
106-
execute_result.to_pandas_batches(page_size=self.page_size),
107-
)
94+
self._batches = self._dataframe.to_pandas_batches(page_size=self.page_size)
95+
self.row_count = self._batches.total_rows or 0
10896

10997
self._set_table_html()
11098
self._initial_load_complete = True
@@ -198,19 +186,7 @@ def _cached_data(self) -> pd.DataFrame:
198186

199187
def _reset_batches_for_new_page_size(self) -> None:
200188
"""Reset the batch iterator when page size changes."""
201-
# Execute with explicit destination for consistency with __init__
202-
execute_result = self._dataframe._block.session._executor.execute(
203-
self._dataframe._block.expr,
204-
execution_spec=bigframes.session.execution_spec.ExecutionSpec(
205-
ordered=True, promise_under_10gb=False
206-
),
207-
)
208-
209-
# Create pandas batches from the ExecuteResult
210-
self._batches = cast(
211-
bigframes.core.blocks.PandasBatches,
212-
execute_result.to_pandas_batches(page_size=self.page_size),
213-
)
189+
self._batches = self._dataframe.to_pandas_batches(page_size=self.page_size)
214190

215191
self._cached_batches = []
216192
self._batch_iter = None

notebooks/dataframes/dataframe.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5366,7 +5366,7 @@
53665366
],
53675367
"metadata": {
53685368
"kernelspec": {
5369-
"display_name": "Python 3 (ipykernel)",
5369+
"display_name": "3.10.18",
53705370
"language": "python",
53715371
"name": "python3"
53725372
},
@@ -5380,7 +5380,7 @@
53805380
"name": "python",
53815381
"nbconvert_exporter": "python",
53825382
"pygments_lexer": "ipython3",
5383-
"version": "3.11.1"
5383+
"version": "3.10.18"
53845384
}
53855385
},
53865386
"nbformat": 4,

0 commit comments

Comments
 (0)