我正在使用Google合作实验室创建要上传到Google BigQuery的表格。我的笔记本收到TypeError:to_gbq()在尝试将数据上传到BigQuery时收到了意外的关键字参数“位置”。代码如下,并抛出错误。
from google.colab import files
from google.colab import drive
from google.colab import auth
from oauth2client.client import GoogleCredentials
drive.mount('/content/gdrive')
Drive already mounted at /content/gdrive; to attempt to forcibly remount, call drive.mount("/content/gdrive", force_remount=True).
import pandas as pd
import io
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
## Usage file
df = pd.read_csv("/content/gdrive/My Drive/test.csv")
df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 4 columns):
Header1 3 non-null object
Header2 3 non-null object
Header3 3 non-null object
Header4 3 non-null object
dtypes: object(4)
memory usage: 176.0+ bytes
df.to_gbq('table.new', 'tableproject-196326', if_exists='replace')
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-4-efe83f801834> in <module>()
----> 1 df.to_gbq('table.new', 'tableproject-196326', if_exists='replace')
1 frames
/usr/local/lib/python3.6/dist-packages/pandas/io/gbq.py in to_gbq(dataframe, destination_table, project_id, chunksize, reauth, if_exists, auth_local_webserver, table_schema, location, progress_bar, credentials, verbose, private_key)
160 auth_local_webserver=auth_local_webserver, table_schema=table_schema,
161 location=location, progress_bar=progress_bar,
--> 162 credentials=credentials, verbose=verbose, private_key=private_key)
TypeError: to_gbq() got an unexpected keyword argument 'location'
df.head()
此错误刚刚开始发生。
有关如何解决此问题的任何帮助。
答案 0 :(得分:1)
尝试改用pandas_gbq,然后从google.oauth2导入service_account。以下代码在Colaboratory中对我有用。
from google.oauth2 import service_account
import pandas_gbq as gbq
gbq.to_gbq(df, 'test_dataset.test_table', 'add_project_id', if_exists='append')