1

I am fetching a google sheet as pandas dataframe, then adding a column in the df and then uploading it back. I am facing this error:

Object of type 'int64' is not JSON serializable

Below is the code:

#reading and writing on google sheets

gc = pygsheets.authorize(service_file="/Projects/big_query_funnel_v1/input/AppFunnelgs-e1968da.json")

sheet=gc.open('Daily Drop Offs Summary')
wks = sheet.worksheet_by_title('New_Funnel')

#converting to pandas df
ds = wks.get_as_df()

#making a new column
ds.insert(loc=1, column=str(dates), value=edf.vals)

print(ds.head())

wks.set_dataframe(ds, 'A1')

I am getting the error in the last line of code i.e while uploading back the df.

NK09
  • 195
  • 1
  • 12

2 Answers2

0

Json doesnt understand the numpy integer, so the dataframe need to be converted to string or int before uploading back to google sheets.

ds = ds.applymap(str)
NK09
  • 195
  • 1
  • 12
0

Following works (no modifications to the logic):

edf = pd.DataFrame({"vals":[1,2,3,4,5]}, dtype="int64")
dates = "column_name"



#reading and writing on google sheets

gc = pygsheets.authorize(service_file="/Projects/big_query_funnel_v1/input/AppFunnelgs-e1968da.json")

sheet=gc.open('Daily Drop Offs Summary')
wks = sheet.worksheet_by_title('New_Funnel')

#converting to pandas df
ds = wks.get_as_df()

#making a new column
ds.insert(loc=1, column=str(dates), value=edf.vals)

print(ds.head())

wks.set_dataframe(ds, 'A1')

Package versions:

pygsheets==2.0.1
pandas==0.23.4
avloss
  • 2,389
  • 2
  • 22
  • 26