为什么会出现警告:“FutureWarning:设置不兼容的数据类型的项目已被弃用,并且会在 pandas 的未来版本中引发错误”?

问题描述 投票:0回答:1

考虑到这种情况,我不明白为什么要提出这个特别的警告。 将函数应用于数字系列时,它会引发

FutureWarning: Setting an item of incompatible dtype is deprecated and will raise an error in a future version of pandas. Value '[0 1 1 ... 1 0 0]' has dtype incompatible with int32, please explicitly cast to a compatible dtype first.

这是正在应用的功能:

def rush_hourizer(hour):
    if 6 <= hour['rush_hour'] < 10:
        val = 1
    elif 16 <= hour['rush_hour'] < 20:
        val = 1
    else:
        val = 0
    return val

这是存储的数据类型和数据:

print(df1['rush_hour'].dtype)
print(df1['rush_hour'].unique())
int32
[ 0 14  7 19 17 15 23  6 13 18 12 20 10 21 11  8  2 16  9 22  1  5  4  3]

所以当我跑步时:

df1.loc[(df1.day != 'saturday') & (df1.day != 'sunday'), 'rush_hour'] = df1.apply(rush_hourizer, axis=1)

我收到了设置提到的不兼容数据类型的 FutureWarning。 鉴于我正在应用一个数值,我不明白为什么会出现此消息。 我错过了什么?

python pandas
1个回答
0
投票

这很可能是 pandas 数据类型的差异。看起来 pandas 在传递原生 Python 整数作为数据时的默认行为是根据

这个示例
并在本地测试它,将其推断为 int64

因此,当您运行 apply 函数并返回本机 python 整数时,pandas 认为您正在传递

int64
。在您的示例中,
rush_hour
字段最初是
int32
,导致了差异。

避免错误的一种方法是在运行

int64
之前将原始列转换为
apply

df1['rush_hour'] = df1['rush_hour'].astype('int64')
© www.soinside.com 2019 - 2024. All rights reserved.