Click here to Skip to main content
15,903,523 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have two DataFrames df1 and df2

df1 = pd.DataFrame(cursor, columns=('Visit_id',
'Lead_Id',
'contact_number',
'Visit_Date',
'City',
'RM',
'call_start_time',
'call_end_time',
'display_name',
'work_type',
'Visit_Scheduled'
)) 

          
df2 = pd.DataFrame(cursor, columns=('campaign_name',
'contact_number',
'start_time',
'end_time',
'call_duration',
'agent_id'

    
    ))

merged_df = df1.merge(df2, on='contact_number', how='left', indicator=True)


gc = pygsheets.authorize(service_file=r"C:/test/Downloads/keys.json")

sh = gc.open('Test')

sheet1=sh.worksheet_by_title('Dump')

sheet1.clear(start='A',end='Q')

sheet1.set_dataframe(merged_df,start=(1,1)) 

print('merged_df') 
I am getting error as below :

Traceback (most recent call last):

  File "C:\Users\ABHISH~1\AppData\Local\Temp/ipykernel_14896/96025778.py", line 1, in <module>
    runfile('C:/Users/test/Pictures/Python Codes/AMEYOTEST.py', wdir='C:/Users/test/Pictures/Python Codes')

  File "C:\ProgramData\Anaconda3\lib\site-packages\debugpy\_vendored\pydevd\_pydev_bundle\pydev_umd.py", line 167, in runfile
    execfile(filename, namespace)

  File "C:\ProgramData\Anaconda3\lib\site-packages\debugpy\_vendored\pydevd\_pydev_imps\_pydev_execfile.py", line 25, in execfile
    exec(compile(contents + "\n", file, 'exec'), glob, loc)

  File "C:/Users/test/Pictures/Python Codes/AMEYOTEST.py", line 192, in <module>
    sheet1.set_dataframe(merged_df,start=(1,1))

  File "C:\ProgramData\Anaconda3\lib\site-packages\pygsheets\utils.py", line 163, in wrapper
    return func(*args, **kwargs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pygsheets\worksheet.py", line 1367, in set_dataframe
    df = df.fillna(nan)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\util\_decorators.py", line 311, in wrapper
    return func(*args, **kwargs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\frame.py", line 5176, in fillna
    return super().fillna(

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\generic.py", line 6382, in fillna
    new_data = self._mgr.fillna(

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals\managers.py", line 410, in fillna
    return self.apply(

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals\managers.py", line 327, in apply
    applied = getattr(b, f)(**kwargs)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\internals\blocks.py", line 1573, in fillna
    values = self.values.fillna(value=value, limit=limit)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\arrays\_mixins.py", line 246, in fillna
    self._validate_setitem_value(value)

  File "C:\ProgramData\Anaconda3\lib\site-packages\pandas\core\arrays\categorical.py", line 2043, in _validate_setitem_value
    raise ValueError(


**ValueError: Cannot setitem on a Categorical with a new category, set the categories first**


What I have tried:

I simply wants to print merged_df values in google sheets
Posted
Updated 5-Nov-21 7:14am

1 solution

I have a similar problem, but my error message is:

ValueError: Cannot set a Categorical with another, without identical categories


In my case the error occurs on a perfectly good pandas.cut statement.

I found that I can eliminate the error by downgrading Pandas from 1.3.4 to 1.2.5 As soon as I try to upgrade Pandas to 1.3.0 I start to get the Categorical error.

Hope this helps.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900