设置不兼容的数据类型的项目已被弃用,并且将在 pandas 的未来错误中引发

Dor*_*ian 8 python pandas

我有下面的代码,例如可以正常工作,但将来不会工作:

    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)
Run Code Online (Sandbox Code Playgroud)

因为抛出了一个错误

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'
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题?

变量总计为:

CURRENCY
MANDATE             Mandate_Test
USD AMOUNT          123
LOCAL AMOUNT        12
Beg. Mkt            123
End. Mkt            456
Name: New_Row, dtype: object
Run Code Online (Sandbox Code Playgroud)

jez*_*ael 4

我认为这是错误 - BUG:使用 loc #55025 创建字符串列时不兼容的数据类型。在 pandas 的下一个版本中应该可以解决。

  • 我认为你是对的,但当我确定时我会接受,以防有人得到其他解释。 (2认同)