Skip to content

BUG: avoid load jobs for empty dataframes #255

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

Merged
merged 3 commits into from
Mar 15, 2019
Merged
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
5 changes: 5 additions & 0 deletions pandas_gbq/gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -1053,6 +1053,11 @@ def to_gbq(
else:
table.create(table_id, table_schema)

if dataframe.empty:
# Create the table (if needed), but don't try to run a load job with an
# empty file. See: https://github.com/pydata/pandas-gbq/issues/237
return

connector.load_data(
dataframe,
dataset_id,
Expand Down
36 changes: 28 additions & 8 deletions tests/system/test_gbq.py
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,8 @@ def test_tokyo(self, tokyo_dataset, tokyo_table, private_key_path):
class TestToGBQIntegration(object):
@pytest.fixture(autouse=True, scope="function")
def setup(self, project, credentials, random_dataset_id):
from google.cloud import bigquery

# - PER-TEST FIXTURES -
# put here any instruction you want to be run *BEFORE* *EVERY* test is
# executed.
Expand All @@ -900,6 +902,9 @@ def setup(self, project, credentials, random_dataset_id):
)
self.destination_table = "{}.{}".format(random_dataset_id, TABLE_ID)
self.credentials = credentials
self.bqclient = bigquery.Client(
project=project, credentials=credentials
)

def test_upload_data(self, project_id):
test_id = "1"
Expand All @@ -926,7 +931,6 @@ def test_upload_data(self, project_id):

def test_upload_empty_data(self, project_id):
test_id = "data_with_0_rows"
test_size = 0
df = DataFrame()

gbq.to_gbq(
Expand All @@ -936,15 +940,31 @@ def test_upload_empty_data(self, project_id):
credentials=self.credentials,
)

result = gbq.read_gbq(
"SELECT COUNT(*) AS num_rows FROM {0}".format(
self.destination_table + test_id
),
project_id=project_id,
table = self.bqclient.get_table(self.destination_table + test_id)
assert table.num_rows == 0
assert len(table.schema) == 0

def test_upload_empty_data_with_schema(self, project_id):
test_id = "data_with_0_rows"
df = DataFrame(
{
"a": pandas.Series(dtype="int64"),
"b": pandas.Series(dtype="object"),
}
)

gbq.to_gbq(
df,
self.destination_table + test_id,
project_id,
credentials=self.credentials,
dialect="legacy",
)
assert result["num_rows"][0] == test_size

table = self.bqclient.get_table(self.destination_table + test_id)
assert table.num_rows == 0
schema = table.schema
assert schema[0].field_type == "INTEGER"
assert schema[1].field_type == "STRING"

def test_upload_data_if_table_exists_fail(self, project_id):
test_id = "2"
Expand Down