Skip to content

Commit 53a6830

Browse files
Saumitra Shahapurejreback
Saumitra Shahapure
authored andcommitted
BUG: Pandas throws exception if Google Bigquery output is empty, pandas-dev#10274
1 parent 66174e5 commit 53a6830

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

doc/source/whatsnew/v0.17.0.txt

+1
Original file line numberDiff line numberDiff line change
@@ -815,3 +815,4 @@ Bug Fixes
815815
- Bug preventing access to the first index when using ``iloc`` with a list containing the appropriate negative integer (:issue:`10547`, :issue:`10779`)
816816
- Bug in ``TimedeltaIndex`` formatter causing error while trying to save ``DataFrame`` with ``TimedeltaIndex`` using ``to_csv`` (:issue:`10833`)
817817
- Bug in ``DataFrame.where`` when handling Series slicing (:issue:`10218`, :issue:`9558`)
818+
- Bug where ``pd.read_gbq`` throws ``ValueError`` when Bigquery returns zero rows (:issue:`10273`)

pandas/io/gbq.py

+6-3
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def get_service(self, credentials):
121121

122122
try:
123123
from apiclient.discovery import build
124-
124+
125125
except ImportError:
126126
raise ImportError('Could not import Google API Client.')
127127

@@ -279,7 +279,7 @@ def _parse_data(schema, rows):
279279
field_type)
280280
page_array[row_num][col_num] = field_value
281281

282-
return DataFrame(page_array)
282+
return DataFrame(page_array, columns=col_names)
283283

284284
def _parse_entry(field_value, field_type):
285285
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
338338
page = pages.pop()
339339
dataframe_list.append(_parse_data(schema, page))
340340

341-
final_df = concat(dataframe_list, ignore_index = True)
341+
if len(dataframe_list) > 0:
342+
final_df = concat(dataframe_list, ignore_index=True)
343+
else:
344+
final_df = _parse_data(schema, [])
342345

343346
# Reindex the DataFrame on the provided column
344347
if index_col is not None:

pandas/io/tests/test_gbq.py

+7
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,13 @@ def test_download_dataset_larger_than_200k_rows(self):
296296
df = gbq.read_gbq("SELECT id FROM [publicdata:samples.wikipedia] GROUP EACH BY id ORDER BY id ASC LIMIT 200005", project_id=PROJECT_ID)
297297
self.assertEqual(len(df.drop_duplicates()), 200005)
298298

299+
def test_zero_rows(self):
300+
# Bug fix for https://github.com/pydata/pandas/issues/10273
301+
df = gbq.read_gbq("SELECT title, language FROM [publicdata:samples.wikipedia] where timestamp=-9999999", project_id=PROJECT_ID)
302+
expected_result = DataFrame(columns=['title', 'language'])
303+
self.assert_frame_equal(df, expected_result)
304+
305+
299306
class TestToGBQIntegration(tm.TestCase):
300307
# This class requires bq.py to be installed for setup/teardown.
301308
# It will also need to be preconfigured with a default dataset,

0 commit comments

Comments
 (0)