8484import bigframes .core .ordering as order
8585import bigframes .core .traversal as traversals
8686import bigframes .core .utils as utils
87- import bigframes .dataframe as dataframe
8887import bigframes .dtypes
8988import bigframes .formatting_helpers as formatting_helpers
9089from bigframes .functions .remote_function import read_gbq_function as bigframes_rgf
9392import bigframes .session .clients
9493import bigframes .version
9594
95+ # Avoid circular imports.
96+ if typing .TYPE_CHECKING :
97+ import bigframes .dataframe as dataframe
98+
9699_BIGFRAMES_DEFAULT_CONNECTION_ID = "bigframes-default-connection"
97100
98101_MAX_CLUSTER_COLUMNS = 4
@@ -557,6 +560,8 @@ def _read_gbq_query(
557560 api_name : str = "read_gbq_query" ,
558561 use_cache : Optional [bool ] = None ,
559562 ) -> dataframe .DataFrame :
563+ import bigframes .dataframe as dataframe
564+
560565 configuration = _transform_read_gbq_configuration (configuration )
561566
562567 if "query" not in configuration :
@@ -754,6 +759,8 @@ def _read_gbq_table(
754759 api_name : str ,
755760 use_cache : bool = True ,
756761 ) -> dataframe .DataFrame :
762+ import bigframes .dataframe as dataframe
763+
757764 if max_results and max_results <= 0 :
758765 raise ValueError ("`max_results` should be a positive number." )
759766
@@ -989,6 +996,8 @@ def read_pandas(self, pandas_dataframe: pandas.DataFrame) -> dataframe.DataFrame
989996 def _read_pandas (
990997 self , pandas_dataframe : pandas .DataFrame , api_name : str
991998 ) -> dataframe .DataFrame :
999+ import bigframes .dataframe as dataframe
1000+
9921001 if isinstance (pandas_dataframe , dataframe .DataFrame ):
9931002 raise ValueError (
9941003 "read_pandas() expects a pandas.DataFrame, but got a "
@@ -1003,6 +1012,8 @@ def _read_pandas(
10031012 def _read_pandas_inline (
10041013 self , pandas_dataframe : pandas .DataFrame
10051014 ) -> Optional [dataframe .DataFrame ]:
1015+ import bigframes .dataframe as dataframe
1016+
10061017 if pandas_dataframe .size > MAX_INLINE_DF_SIZE :
10071018 return None
10081019
@@ -1024,11 +1035,20 @@ def _read_pandas_inline(
10241035 def _read_pandas_load_job (
10251036 self , pandas_dataframe : pandas .DataFrame , api_name : str
10261037 ) -> dataframe .DataFrame :
1038+ import bigframes .dataframe as dataframe
1039+
1040+ col_index = pandas_dataframe .columns .copy ()
10271041 col_labels , idx_labels = (
1028- pandas_dataframe . columns .to_list (),
1042+ col_index .to_list (),
10291043 pandas_dataframe .index .names ,
10301044 )
1031- new_col_ids , new_idx_ids = utils .get_standardized_ids (col_labels , idx_labels )
1045+ new_col_ids , new_idx_ids = utils .get_standardized_ids (
1046+ col_labels ,
1047+ idx_labels ,
1048+ # Loading parquet files into BigQuery with special column names
1049+ # is only supported under an allowlist.
1050+ strict = True ,
1051+ )
10321052
10331053 # Add order column to pandas DataFrame to preserve order in BigQuery
10341054 ordering_col = "rowid"
@@ -1047,7 +1067,7 @@ def _read_pandas_load_job(
10471067
10481068 # Specify the datetime dtypes, which is auto-detected as timestamp types.
10491069 schema : list [bigquery .SchemaField ] = []
1050- for column , dtype in zip (pandas_dataframe . columns , pandas_dataframe .dtypes ):
1070+ for column , dtype in zip (new_col_ids , pandas_dataframe .dtypes ):
10511071 if dtype == "timestamp[us][pyarrow]" :
10521072 schema .append (
10531073 bigquery .SchemaField (column , bigquery .enums .SqlTypeNames .DATETIME )
@@ -1101,7 +1121,7 @@ def _read_pandas_load_job(
11011121 block = blocks .Block (
11021122 array_value ,
11031123 index_columns = new_idx_ids ,
1104- column_labels = col_labels ,
1124+ column_labels = col_index ,
11051125 index_labels = idx_labels ,
11061126 )
11071127 return dataframe .DataFrame (block )
0 commit comments