【问题标题】:Why pd.concat() of two dataframe leads to FutureWarning: Behavior when concatenating bool-dtype?Why pd.concat() of two dataframe leads to FutureWarning: Behavior when concatenating bool-dtype?
【发布时间】:2022-12-27 16:30:05
【问题描述】:

List item

I want to concatenate two dataframe with pd.concat() as below:

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

It returns the following warning:

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.

I have read this question as well, but here I have a dataframe which contains string and different types of numbers (integer, float). What should I do in this case?

This is the sample data of each dataframe: df_1:

dateTime entryRate stop limit amount stdLotds currencyName Buy
3/11/2022 11:24 1.31006 0 0 5000 0.05 GBPUSD True
3/11/2022 11:24 1.31007 0 0 1000 0.01 GBPUSD False
3/11/2022 11:11 1.79134 0 1.78448 2000 0.02 GBPAUD True

df_2:

dateTime entryRate stop limit amount stdLotds currencyName Buy
3/14/2022 10:24 1.31012 0 0 5000 0.05 GBPUSD False
3/11/2022 12:25 1.31017 0 0 3000 0.09 EURUSD False
3/14/2022 10:00 1.79114 0 1.78448 2000 0.03 AUDCAD True

【问题讨论】:

  • Please include sample data so others may try to re-create problem and better understand how to help
  • It is saying one of your columns in one of the df is defined as bool, while the same column on the other df is defined as numeric. Have you checked your dfs if this is the case? If yes, you should do as the warning says and ensure the columns have same data types before concatenating.
  • I face this warning even one of the dataframe is empty! Does it meaningful?
  • Why are you concatenating if one of your dataframes is empty? Is that not what this line >> if (not df_1.empty) | (not df_2.empty): << checks for?
  • try to do printouts of df.info() and post them both in the question

标签: python pandas


【解决方案1】:

I got this warning in an unexpected location when concatenating a list of dataframes that all contained the same columns with consistent datatypes across frames. The reason the warning appeared was that some frames had 0 rows and therefore were not assigned datatypes in the same way as the other frames. I stopped receiving the error after filtering out empty dataframes before concatenating.

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-12-27
    • 1970-01-01
    • 1970-01-01
    • 2022-12-01
    • 2022-12-27
    • 2022-01-29
    • 1970-01-01
    • 2022-12-02
    相关资源
    最近更新 更多