【问题标题】:TypeError to_gbq() got an unexpected keyword argument 'location'TypeError to_gbq() 得到了一个意外的关键字参数“位置”
【发布时间】:2019-09-15 07:03:48
【问题描述】:

我正在使用 Google Colaboratory 创建要上传到 Google BigQuery 的表。我的笔记本在尝试将数据上传到 BigQuery 时收到 TypeError:to_gbq() got an unexpected keyword argument 'location'。代码如下,抛出错误。


    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()

这个错误刚刚开始发生。

有关如何解决此问题的任何帮助。

【问题讨论】:

  • 没有代码,我们和你一样一无所知
  • 我刚刚更新了问题以包含代码

标签: pandas google-colaboratory


【解决方案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')

【讨论】:

  • 谢谢,这解决了 TypeError to_gbq() got an unexpected keyword argument 'location' 问题,但现在我有另一个问题。 to_gbq 不允许引用换行符。不知道在使用gbq.to_gbq的时候有没有开关可以开启?
  • 解决了我的第二个问题。我将 pandas_gbq 更新为当前版本,现在一切正常。
  • 解决了我的第二个问题。我使用 !pip install pandas_gbq -u 将 pandas_gbq 更新为当前版本,现在一切正常。
猜你喜欢
  • 1970-01-01
  • 2016-09-17
  • 2012-12-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-08
相关资源
最近更新 更多