我正在尝试通过pandas将一个表附加到另一个表,从BigQuery中提取数据并将其发送到另一个BigQuery dataset。虽然表模式完全相同,但我收到错误“”请验证结构和“pandas_gbq.gbq.InvalidSchema:请验证DataFrame中的结构和数据类型是否与目标表的模式匹配。”
这个错误发生在我之前改写表的地方,但在这种情况下,数据集太大,无法做到这一点(这不是一个可持续的解决方案)。
df = pd.read_gbq(query, project_id="my-project", credentials=bigquery_key,
dialect='standard')
pd.io.gbq.to_gbq(df, dataset, projectid,
if_exists='append',
table_schema=[{'name': 'Date','type': 'STRING'},
{'name': 'profileId','type': 'STRING'},
{'name': 'Opco','type': 'STRING'},
{'name': 'country','type': 'STRING'},
{'name': 'deviceType','type': 'STRING'},
{'name': 'userType','type': 'STRING'},
{'name': 'users','type': 'INTEGER'},
{'name': 'sessions','type': 'INTEGER'},
{'name': 'bounceRate','type': 'FLOAT'},
{'name': 'sessionsPerUser','type': 'FLOAT'},
{'name': 'avgSessionDuration','type': 'FLOAT'},
{'name': 'pageviewsPerSession','type': 'FLOAT'}
],
credentials=bigquery_key)BigQuery中的模式如下:
Date STRING
profileId STRING
Opco STRING
country STRING
deviceType STRING
userType STRING
users INTEGER
sessions INTEGER
bounceRate FLOAT
sessionsPerUser FLOAT
avgSessionDuration FLOAT
pageviewsPerSession FLOAT 然后,我得到以下错误:
Traceback (most recent call last): File "..file.py", line 63, in
<module>
main()
File "..file.py", line 57, in main
updating_general_data(bigquery_key)
File "..file.py", line 46, in updating_general_data
credentials=bigquery_key)
File
"..\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\io\gbq.py",
line 162, in to_gbq
credentials=credentials, verbose=verbose, private_key=private_key)
File
"..\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas_gbq\gbq.py",
line 1141, in to_gbq
"Please verify that the structure and " pandas_gbq.gbq.InvalidSchema: Please verify that the structure and
data types in the DataFrame match the schema of the destination table.在我看来,这是一场一对一的比赛。我见过其他线程讨论这个问题,这些线程主要讨论日期格式,尽管在本例中日期格式已经是一个字符串,并且table_schema仍然是字符串。
发布于 2019-12-05 20:07:06
出现问题的原因很可能是DataFrame和模式中的列名不匹配
发布于 2021-03-30 09:39:41
我的数据框中有一个名为“No”的列。删除句点可解决此问题,并推断出架构。
发布于 2021-09-28 18:57:28
我在这方面遇到了真正的问题,并通过让pandas-gbq创建数据库来修复它,而不是在UI中创建数据库并尝试匹配模式
https://stackoverflow.com/questions/56545738
复制相似问题