将系列作为新行添加到 DataFrame 中会触发 FutureWarning

alg*_*ell 7 python series dataframe pandas

尝试将新的类型行添加Series到 a 中DataFrame,两者共享相同的列/索引:

df.loc[df.shape[0]] = r
Run Code Online (Sandbox Code Playgroud)

得到:

未来警告:在未来版本中,具有全布尔值的对象数据类型列将不会包含在 bool_only=True 的缩减中。显式转换为 bool dtype。

其中来自推理模块

rok*_*kdd 17

我遇到了同样的错误,这是因为pandas 的版本为 1.5.0,为什么这里的一些答案可能无法解决问题:

不建议在 DataFrame.any() 和 DataFrame.all() 中使用 bool_only=True 将所有 bool 对象数据类型列视为 bool 类型,而是显式转换为 bool (GH46188)

所以我试图理解......但不知何故我能够找到解决方案。原因是具有布尔值的列未正确转换。我使用了 concat,对我来说它是现有的 DataFrame。

因为我不想为 Dataframe 的所有列定义相应的 dtype(这也可能),所以我对必要的列进行了更改:

df["var1"]=df["var1"].astype(bool)
Run Code Online (Sandbox Code Playgroud)

或者对于多个:

df=df.astype({"var1":bool,"var2":bool})
Run Code Online (Sandbox Code Playgroud)

然后 concat 在没有 FutureWarning 的情况下为我工作。


kha*_*baa 0

尝试:

df
    c1  c2  c3      c4
0   1   3   True    abc
1   2   4   False   def

d = {'c1': 3, 'c2': 5, 'c3': True, 'c4': 'ghi'} 
s = pd.Series(d) 

s
c1       3
c2       5
c3    True
c4     ghi
dtype: object

df.loc[df.shape[0]] = s.to_numpy() 

df
    c1  c2  c3      c4
0   1   3   True    abc
1   2   4   False   def
2   3   5   True    ghi

Run Code Online (Sandbox Code Playgroud)