question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`to_gbq` fails to create empty table with correct schema

See original GitHub issue

I want to create an empty table that has a specific schema, which in this specific case I’m inferring from a dask dataframe. The table gets created on BigQuery, however it shows with no schema.

Minimal reproducible example:

import pandas_gbq
import dask.dataframe as dd
import random 

#generate some data
def gen_data(size=10):
    records = [
        {
            "name": random.choice(["fred", "wilma", "barney", "betty"]),
            "number": random.randint(0, 100),
            "idx": i,
        }
        for i in range(size)
    ]
    return records

test= gen_data(size=50)
df = pd.DataFrame(test)

ddf = dd.from_pandas(df, chunksize=10)

meta = ddf._meta.dtypes.to_dict()
schema = [{"name":k, "type": v} for k, v in meta.items()]

#the schema looks like this
# [{'name': 'name', 'type': dtype('O')},
# {'name': 'number', 'type': dtype('int64')},
#  {'name': 'idx', 'type': dtype('int64')}]

destination_table="my_dataset.test_table"
project_id="my_project"

pandas_gbq.to_gbq(dataframe=pd.DataFrame(), destination_table=destination_table, 
          project_id=project_id, if_exists="append", table_schema=schema)

But when I check the schema on the table created I get:

Screen Shot 2021-08-12 at 5 30 39 PM

Environment details

  • Programming language: python 3.3.10
  • OS: MacOS Big Sur 11.4
  • Package version: pandas-gbq 0.15.0 (pypi)
  • pandas version: 1.3.1
  • Dask version: 2021.07.2

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
jimfultoncommented, Sep 3, 2021

You want something like:

schema = [
    {'name': 'name', 'type': 'STRING'},
    {'name': 'number', 'type': 'int64'},
    {'name': 'idx', 'type': 'int64'},
]
pandas_gbq.to_gbq(
    pd.DataFrame({field['name']: [] for field in schema}),
    destination_table=destination_table, 
    project_id=project_id,
    if_exists="append",
    table_schema=schema)
1reaction
tswastcommented, Sep 2, 2021

Thanks for the report! I agree this looks like a bug.

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to create an empty table with specific schema in GBQ?
json file and I want to create an empty table with it, in Google Big Query. I am using pandas_gbq.to_gbq(dataframe=None, destination_table="{}.
Read more >
pandas.DataFrame.to_gbq — pandas 1.5.2 documentation
Set to None to load the whole dataframe at once. ... 'fail'. If table exists raise pandas_gbq.gbq.TableCreationError. ... progress_barbool, default True.
Read more >
Bug in if_exists='replace' · Issue #91 - GitHub
The tables have the same schema and no error about schema is displayed. After the first write, the table seems empty but there's...
Read more >
How to create an empty table with specific schema in GBQ?
The method is expecting to receive a pandas.DataFrame object to work with, that's why using a None object from python is breaking your...
Read more >
pandas-gbq Documentation - Read the Docs
Set use_bqstorage_api to True when calling the read_gbq() function. ... The to_gbq() method infers the BigQuery table schema based on the ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found