`to_gbq` fails to create empty table with correct schema
See original GitHub issueI 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:

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:
- Created 2 years ago
- Comments:8 (3 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

You want something like:
Thanks for the report! I agree this looks like a bug.