pandas 为什么两个 Dataframe 的pd.concat()会导致FutureWarning:连接bool-dtype时的行为?

e0bqpujr  于 2022-12-28  发布在  其他
关注(0)|答案(2)|浏览(518)

列表项
我想用pd.concat()连接两个 Dataframe ,如下所示:

if (not df_1.empty) | (not df_2.empty):
    new_df= pd.concat([df_1, df_2])

它返回以下警告:

FutureWarning: Behavior when concatenating bool-dtype and numeric-dtype arrays is deprecated; in a future version these will cast to object dtype (instead of coercing bools to numeric values). To retain the old behavior, explicitly cast bool-dtype arrays to numeric dtype.

我也读过this问题,但这里有一个DataFrame,其中包含str和不同类型的数字(intfloat)。在这种情况下,我应该怎么做?
以下是每个 Dataframe 的样本数据:df_1:
| 日期时间|入学率|停止|极限|数量|标准批次|货币名称|购买|
| - ------| - ------| - ------| - ------| - ------| - ------| - ------| - ------|
| 二〇二二年十一月三日十一时二十四分|1.31006|无|无|五千|0.05分|英镑美元|真的|
| 二〇二二年十一月三日十一时二十四分|1.31零零七|无|无|一千|0.01分|英镑美元|假|
| 2022年3月11日11时11分|一点七九一三四|无|一点七八四四八|二○ ○ ○年|0.02分|GBPAUD公司|真的|
df_2:
| 日期时间|入学率|停止|极限|数量|标准批次|货币名称|购买|
| - ------| - ------| - ------| - ------| - ------| - ------| - ------| - ------|
| 二〇二二年三月十四日十时二十四分|1.31012美元|无|无|五千|0.05分|英镑美元|假|
| 二〇二二年三月十一日十二时二十五分|1.31017美元|无|无|三千人|0.09|欧元/美元|假|
| 2022年3月14日上午10时|一点七九一一四|无|一点七八四四八|二○ ○ ○年|0.03分|澳大利亚民航总局|真的|

fv2wmkja

fv2wmkja1#

当连接 Dataframe 列表时,我在一个意外的位置收到这个警告,这些 Dataframe 都包含相同的列,并且跨帧的数据类型一致。出现警告的原因是一些帧有0行,因此没有以与其他帧相同的方式分配数据类型。在连接之前过滤掉空 Dataframe 后,我停止接收错误。

llmtgqce

llmtgqce2#

应该是

if (not df_1.empty) & (not df_2.empty):
    new_df= pd.concat([df_1, df_2])

即用和(&)代替或(|)

相关问题