Setting an item of incompatible dtype is deprecated and will raise in a future error of pandas

Question:

I have the below code which for instance work as excepted but won’t work in the Future :

    total.name = 'New_Row'
    total_df = total.to_frame().T
    total_df.at['New_Row', 'CURRENCY'] = ''
    total_df.at['New_Row', 'MANDATE'] = Portfolio
    total_df.at['New_Row', 'COMPOSITE'] = 'GRAND TOTAL'
    total_df.set_index('COMPOSITE',inplace=True)

since an error is thrown in

FutureWarning: Setting an item of incompatible dtype is deprecated and 
will raise in a future error of pandas. Value 'GRAND TOTAL' has dtype incompatible with float64, please explicitly cast to a compatible dtype first.
  total_df.at['New_Row', 'COMPOSITE'] = 'GRAND TOTAL'

How to fix this ?

variable total is :

CURRENCY
MANDATE             Mandate_Test
USD AMOUNT          123
LOCAL AMOUNT        12
Beg. Mkt            123
End. Mkt            456
Name: New_Row, dtype: object
Asked By: TourEiffel

||

Answers:

I think it is bug – BUG: incompatible dtype when creating string column with loc #55025 . In next version of pandas should be solved.

Answered By: jezrael

Convert when open, for ex:
df_new = pd.read_excel(df_new, converters={‘Title’: str, ‘Description’: str})

Answered By: Denis

I had the same problem. I solved it by setting the whole column to None and then making it string type. This should probably also work with rows (as in your case) or cells etc.

df[‘CURRENCY’] = None

df[‘CURRENCY’] = df[‘CURRENCY’].astype(str)

Answered By: Cédric
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.