DeprecationWarning:在未来的版本警告中,空系列的默认 dtype 将是“object”而不是“float64”

jsc*_*tor 4 python pandas

我将新行附加到现有的熊猫数据框,如下所示:

df= df.append(pd.Series(), ignore_index=True)
Run Code Online (Sandbox Code Playgroud)

这导致主题 DeprecationWarning。

现有的 df 混合了 string、float 和 dateime.date 数据类型(总共 8 列)。

有没有办法在 df.append 中显式指定列类型?

我已经看过herehere,但我仍然没有解决方案。请告知是否有更好的方法将行附加到现有数据帧的末尾而不触发此警告。

小智 19

您可以添加dtype到您的代码中。

pd.Series(dtype='float64')


小智 8

你可以试试这个

Type_new = pd.Series([],dtype=pd.StringDtype()) 
Run Code Online (Sandbox Code Playgroud)

这将为我们创建一个空白数据框。


小智 6

df = df.append(pd.Series(dtype = 'object'), ignore_index=True)
Run Code Online (Sandbox Code Playgroud)