解决有关dtype是对象的MultiIndex错误

时间:2019-04-10 16:04:34

标签: python mysql pandas google-sheets-api gspread

当尝试使用Google Drive API将枢轴数据帧上传到Google表格时,系统提示我出现以下错误:

TypeError:不支持将class'pandas.core.indexes.multi.MultiIndex'dtype设置为对象以外的任何其他类型

在检查dtypes时,我发现索引值是对象,并且已经将列值也转换为oject,但是我仍然遇到错误。

import gameData_connection
import pandas as pd
import gspread
from oauth2client.service_account import ServiceAccountCredentials
from df2gspread import df2gspread as d2g

scope = ['https://spreadsheets.google.com/feeds']
credentials = ServiceAccountCredentials.from_json_keyfile_name('credentials_files.json', scope)
gc = gspread.authorize(credentials)
spreadsheet_key = 'my_spreadsheet_key'
wks_name = 'My File Name'

connection = gameData_connection.getConnection()

print('Connect Succesful')

try:
    with connection.cursor() as cursor:
        sql1 = This is my whole sql query

cursor.execute(sql1)
        df1 = pd.read_sql_query(sql1, connection)
        print('cursor.description:', cursor.description)
        print()

        for row in cursor:
            print(row)

finally:
    connection.close()



table1 = pd.pivot_table(df1, index = ["game", "platform"], 
                        values = ["new_users", "dau", "day1_retention", "day6_retention", "revenue", "arpdau", "arppu", "pau"],
                        columns = ["year_time", "month_time"])


d2g.upload(table1, spreadsheet_key, wks_name, credentials=credentials, row_names=True) 
print(table1)
Out[21]: 
time_join           int64
year_time           int64
month_time          int64
game               object
platform           object
new_users         float64
dau               float64
day1_retention    float64
day6_retention    float64
revenue           float64
arpdau            float64
arppu             float64
pau               float64
dtype: object

我无法弄清楚我在做什么错。如果我运行d2g.upload(df1, spreadsheet_key, wks_name, credentials=credentials, row_names=True),那么它可以正常工作,因此table1内部一定有问题。

现在请注意,如果将相同的table1写入excel即可。

在此先感谢您的帮助

0 个答案:

没有答案