diff --git a/doc/source/whatsnew/v0.17.0.txt b/doc/source/whatsnew/v0.17.0.txt index 1079ec52338b9..bd4e02a7617f7 100644 --- a/doc/source/whatsnew/v0.17.0.txt +++ b/doc/source/whatsnew/v0.17.0.txt @@ -678,3 +678,4 @@ Bug Fixes - Bug in ``iloc`` allowing memory outside bounds of a Series to be accessed with negative integers (:issue:`10779`) - Bug in ``read_msgpack`` where encoding is not respected (:issue:`10580`) - Bug preventing access to the first index when using ``iloc`` with a list containing the appropriate negative integer (:issue:`10547`, :issue:`10779`) +- Bug where ``pd.read_gbq`` throws ``ValueError`` when Bigquery returns zero rows (:issue:`10273`) diff --git a/pandas/io/gbq.py b/pandas/io/gbq.py index 06ad8827a5642..79706ec0c2990 100644 --- a/pandas/io/gbq.py +++ b/pandas/io/gbq.py @@ -279,7 +279,7 @@ def _parse_data(schema, rows): field_type) page_array[row_num][col_num] = field_value - return DataFrame(page_array) + return DataFrame(page_array, columns=col_names) def _parse_entry(field_value, field_type): if field_value is None or field_value == 'null': @@ -338,7 +338,10 @@ def read_gbq(query, project_id=None, index_col=None, col_order=None, reauth=Fals page = pages.pop() dataframe_list.append(_parse_data(schema, page)) - final_df = concat(dataframe_list, ignore_index = True) + if len(dataframe_list)>0: + final_df = concat(dataframe_list, ignore_index = True) + else: + final_df = _parse_data(schema,[]) # Reindex the DataFrame on the provided column if index_col is not None: diff --git a/pandas/io/tests/test_gbq.py b/pandas/io/tests/test_gbq.py index 5417842d3f863..f04eeb03f790e 100644 --- a/pandas/io/tests/test_gbq.py +++ b/pandas/io/tests/test_gbq.py @@ -296,6 +296,13 @@ def test_download_dataset_larger_than_200k_rows(self): df = gbq.read_gbq("SELECT id FROM [publicdata:samples.wikipedia] GROUP EACH BY id ORDER BY id ASC LIMIT 200005", project_id=PROJECT_ID) self.assertEqual(len(df.drop_duplicates()), 200005) + def test_zero_rows(self): + # Bug fix for https://github.com/pydata/pandas/issues/10273 + df = gbq.read_gbq("SELECT title, language FROM [publicdata:samples.wikipedia] where timestamp=-9999999", project_id=PROJECT_ID) + expected_result = DataFrame(columns=['title', 'language']) + self.assert_frame_equal(df, expected_result) + + class TestToGBQIntegration(tm.TestCase): # This class requires bq.py to be installed for setup/teardown. # It will also need to be preconfigured with a default dataset,