'Pandas Value Error: Cannot set item on a Categorical with a new category, set the categories first
I've been looking for other similar issues on this ValueError, but none of them has the same code as I have. So here it is. As I am still very new at this, I am trying to format data from Google Sheets in Pandas and upload it back to Google Sheets via Google API. It all works fine until I decide to set some columns as categories in order to save RAM (solely purpose).
import pandas as pd
import pygsheets as pyg
import numpy as np
import gspread as gs
gc = pyg.authorize(service_file = r'C:\Users\user\anaconda3\Lib\site-packages\gspread\somefile.json
sh = gc.open('TestPy')
wks = sh[0]
sheet_url = 'https://docs.google.com/spreadsheets/d/XXXXXXXXXXXXXXXXXXXXXXXXX/edit?usp=sharing'
url_1 = sheet_url.replace('/edit?usp=sharing','/export?format=csv&gid=0')
df = pd.read_csv(url_1)
I use the API and an URL for the same Sheet as I have failed to convert the API data to a DF. Please feel free to enlighten my lost soul on the matter.
So now if I decide to upload the DF to Sheets via API, I have no problem.
wks.set_dataframe(df,(1,1))
However, when I set columns as categories:
df['TL name'] = df['TL name'].astype('category')
df['Team'] = df['Team'].astype('category')
df['OM Name'] = df['OM Name'].astype('category')
df['Role'] = df['Role'].astype('category')
df['Day/Night Shift'] = df['Day/Night Shift'].astype('category')
and then try to upload, I get:
ValueError: Cannot setitem on a Categorical with a new category, set the categories first
Any help will be greatly appreciated!
Thank you all in advance!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|