Skip to content

BUG: Unhandled ValueError when Bigquery called through io.gbq returns zero rows #10273 #10274

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/source/whatsnew/v0.17.0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`)
7 changes: 5 additions & 2 deletions pandas/io/gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should not be necessary, page_array is already a record array (this will just reindex it) (and copy it).


def _parse_entry(field_value, field_type):
if field_value is None or field_value == 'null':
Expand Down Expand Up @@ -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,[])
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes it necessary. Since we are constructing blank dataframe, no data is added in dataframe. But it should pick the output schema from query response received from bigquery.


# Reindex the DataFrame on the provided column
if index_col is not None:
Expand Down
7 changes: 7 additions & 0 deletions pandas/io/tests/test_gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down