3737 Optional ,
3838 Sequence ,
3939 Tuple ,
40+ TYPE_CHECKING ,
4041 Union ,
4142)
4243import warnings
6970from bigframes .session import dry_runs , execution_spec
7071from bigframes .session import executor as executors
7172
73+ if TYPE_CHECKING :
74+ from bigframes .session .executor import ExecuteResult
75+
7276# Type constraint for wherever column labels are used
7377Label = typing .Hashable
7478
@@ -404,13 +408,15 @@ def reset_index(
404408 col_level : Union [str , int ] = 0 ,
405409 col_fill : typing .Hashable = "" ,
406410 allow_duplicates : bool = False ,
411+ replacement : Optional [bigframes .enums .DefaultIndexKind ] = None ,
407412 ) -> Block :
408413 """Reset the index of the block, promoting the old index to a value column.
409414
410415 Arguments:
411416 level: the label or index level of the index levels to remove.
412417 name: this is the column id for the new value id derived from the old index
413- allow_duplicates:
418+ allow_duplicates: if false, duplicate col labels will result in error
419+ replacement: if not null, will override default index replacement type
414420
415421 Returns:
416422 A new Block because dropping index columns can break references
@@ -425,23 +431,19 @@ def reset_index(
425431 level_ids = self .index_columns
426432
427433 expr = self ._expr
434+ replacement_idx_type = replacement or self .session ._default_index_type
428435 if set (self .index_columns ) > set (level_ids ):
429436 new_index_cols = [col for col in self .index_columns if col not in level_ids ]
430437 new_index_labels = [self .col_id_to_index_name [id ] for id in new_index_cols ]
431- elif (
432- self .session ._default_index_type
433- == bigframes .enums .DefaultIndexKind .SEQUENTIAL_INT64
434- ):
438+ elif replacement_idx_type == bigframes .enums .DefaultIndexKind .SEQUENTIAL_INT64 :
435439 expr , new_index_col_id = expr .promote_offsets ()
436440 new_index_cols = [new_index_col_id ]
437441 new_index_labels = [None ]
438- elif self . session . _default_index_type == bigframes .enums .DefaultIndexKind .NULL :
442+ elif replacement_idx_type == bigframes .enums .DefaultIndexKind .NULL :
439443 new_index_cols = []
440444 new_index_labels = []
441445 else :
442- raise ValueError (
443- f"Unrecognized default index kind: { self .session ._default_index_type } "
444- )
446+ raise ValueError (f"Unrecognized default index kind: { replacement_idx_type } " )
445447
446448 if drop :
447449 # Even though the index might be part of the ordering, keep that
@@ -630,15 +632,17 @@ def to_pandas(
630632 max_download_size , sampling_method , random_state
631633 )
632634
633- df , query_job = self ._materialize_local (
635+ ex_result = self ._materialize_local (
634636 materialize_options = MaterializationOptions (
635637 downsampling = sampling ,
636638 allow_large_results = allow_large_results ,
637639 ordered = ordered ,
638640 )
639641 )
642+ df = ex_result .to_pandas ()
643+ df = self ._copy_index_to_pandas (df )
640644 df .set_axis (self .column_labels , axis = 1 , copy = False )
641- return df , query_job
645+ return df , ex_result . query_job
642646
643647 def _get_sampling_option (
644648 self ,
@@ -746,7 +750,7 @@ def _copy_index_to_pandas(self, df: pd.DataFrame) -> pd.DataFrame:
746750
747751 def _materialize_local (
748752 self , materialize_options : MaterializationOptions = MaterializationOptions ()
749- ) -> Tuple [ pd . DataFrame , Optional [ bigquery . QueryJob ]] :
753+ ) -> ExecuteResult :
750754 """Run query and download results as a pandas DataFrame. Return the total number of results as well."""
751755 # TODO(swast): Allow for dry run and timeout.
752756 under_10gb = (
@@ -815,8 +819,7 @@ def _materialize_local(
815819 MaterializationOptions (ordered = materialize_options .ordered )
816820 )
817821 else :
818- df = execute_result .to_pandas ()
819- return self ._copy_index_to_pandas (df ), execute_result .query_job
822+ return execute_result
820823
821824 def _downsample (
822825 self , total_rows : int , sampling_method : str , fraction : float , random_state
0 commit comments