FutureWarning:关于 `df['col'].apply(p.Series)`

Igo*_*nko 3 pandas

 FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version. Specify a dtype explicitly to silence this warning.
Run Code Online (Sandbox Code Playgroud)

在做的时候

rc = dataset_ex[column_name].apply(p.Series)

如何避免警告?df.apply(pd.Series)当一个人真正发生的事情意味着什么?

Laz*_*yer 8

这个问题(DeprecationWarning:空系列的默认数据类型将是“object”而不是“float64”在未来版本警告中)与您的问题非常相似,因此它会对您有所帮助。

您可以使用下面的代码忽略所有 python 警告或 FutureWarning,但它只是隐藏警告,而不是解决它。

import warnings
warnings.filterwarnings("ignore")
Run Code Online (Sandbox Code Playgroud)
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
Run Code Online (Sandbox Code Playgroud)

现在,Series 的默认 dtypefloat64但它将更改为object

import pandas as pd
print(pd.__version__)  # '1.2.4'
print(pd.Series().dtype)  # dtype('float64') --> dtype('object') in the future
Run Code Online (Sandbox Code Playgroud)

如果您定义系列的数据类型,警告将消失。

rc = dataset_ex[column_name].apply(lambda x: pd.Series(x, dtype="float"))
Run Code Online (Sandbox Code Playgroud)

或者

rc = dataset_ex[column_name].apply(lambda x: pd.Series(x, dtype="object"))
Run Code Online (Sandbox Code Playgroud)