@@ -2014,6 +2014,68 @@ def test_to_arrow_w_pyarrow_none(self):
20142014 with self .assertRaises (ValueError ):
20152015 row_iterator .to_arrow ()
20162016
2017+ @unittest .skipIf (pandas is None , "Requires `pandas`" )
2018+ def test_to_dataframe_iterable (self ):
2019+ from google .cloud .bigquery .schema import SchemaField
2020+ import types
2021+
2022+ schema = [
2023+ SchemaField ("name" , "STRING" , mode = "REQUIRED" ),
2024+ SchemaField ("age" , "INTEGER" , mode = "REQUIRED" ),
2025+ ]
2026+
2027+ path = "/foo"
2028+ api_request = mock .Mock (
2029+ side_effect = [
2030+ {
2031+ "rows" : [{"f" : [{"v" : "Bengt" }, {"v" : "32" }]}],
2032+ "pageToken" : "NEXTPAGE" ,
2033+ },
2034+ {"rows" : [{"f" : [{"v" : "Sven" }, {"v" : "33" }]}]},
2035+ ]
2036+ )
2037+
2038+ row_iterator = self ._make_one (
2039+ _mock_client (), api_request , path , schema , page_size = 1 , max_results = 5
2040+ )
2041+ dfs = row_iterator .to_dataframe_iterable ()
2042+
2043+ self .assertIsInstance (dfs , types .GeneratorType )
2044+
2045+ df_1 = next (dfs )
2046+ self .assertIsInstance (df_1 , pandas .DataFrame )
2047+ self .assertEqual (df_1 .name .dtype .name , "object" )
2048+ self .assertEqual (df_1 .age .dtype .name , "int64" )
2049+ self .assertEqual (len (df_1 ), 1 ) # verify the number of rows
2050+ self .assertEqual (
2051+ df_1 ["name" ][0 ], "Bengt"
2052+ ) # verify the first value of 'name' column
2053+ self .assertEqual (df_1 ["age" ][0 ], 32 ) # verify the first value of 'age' column
2054+
2055+ df_2 = next (dfs )
2056+ self .assertEqual (len (df_2 ), 1 ) # verify the number of rows
2057+ self .assertEqual (df_2 ["name" ][0 ], "Sven" )
2058+ self .assertEqual (df_2 ["age" ][0 ], 33 )
2059+
2060+ @mock .patch ("google.cloud.bigquery.table.pandas" , new = None )
2061+ def test_to_dataframe_iterable_error_if_pandas_is_none (self ):
2062+ from google .cloud .bigquery .schema import SchemaField
2063+
2064+ schema = [
2065+ SchemaField ("name" , "STRING" , mode = "REQUIRED" ),
2066+ SchemaField ("age" , "INTEGER" , mode = "REQUIRED" ),
2067+ ]
2068+ rows = [
2069+ {"f" : [{"v" : "Phred Phlyntstone" }, {"v" : "32" }]},
2070+ {"f" : [{"v" : "Bharney Rhubble" }, {"v" : "33" }]},
2071+ ]
2072+ path = "/foo"
2073+ api_request = mock .Mock (return_value = {"rows" : rows })
2074+ row_iterator = self ._make_one (_mock_client (), api_request , path , schema )
2075+
2076+ with pytest .raises (ValueError , match = "pandas" ):
2077+ row_iterator .to_dataframe_iterable ()
2078+
20172079 @unittest .skipIf (pandas is None , "Requires `pandas`" )
20182080 def test_to_dataframe (self ):
20192081 from google .cloud .bigquery .schema import SchemaField
0 commit comments