FutureWarning:在未来版本中,空 Series 的默认 dtype 将是 'object' 而不是 'float64'

FutureWarning: The default dtype for empty Series will be 'object' instead of 'float64' in a future version

提问人:Dark Invader 提问时间:2/28/2023 最后编辑:Dark Invader 更新时间:2/28/2023 访问量:487

问:

命令- df = pd.concat([df, df[“XYZ”].apply(pd.系列)],轴=1)

错误-

FutureWarning:在未来版本中,空 Series 的默认 dtype 将是 'object' 而不是 'float64'。显式指定 dtype 以静音此警告。

XYZ 是具有有序字典的列,并试图提取键 = Name 的相应值 命令工作正常,但会引发未来警告。

示例数据 -

同上 XYZ型
1 OrderedDict([('属性', OrderedDict([('类型', 'XYZ'), ('url', 'somelink')])), ('Name', 'ABC')])
2 OrderedDict([('属性', OrderedDict([('类型', 'XYZ'), ('url', 'somelink')])), ('Name', 'PQR')])
Python Pandas 警告 未来 系列

评论


答:

0赞 Daweo 2/28/2023 #1

显式指定 dtype 以静音此警告。

您可以通过以下方式隐式告知应该使用什么dtypepd.Series

import functools
import pandas as pd
Float64Series = functools.partial(pd.Series,dtype="float64")
s = Float64Series([1,2,3,4,5])  # these would be turned into int64 if you would use pd.Series
print(s)

给出输出

0    1.0
1    2.0
2    3.0
3    4.0
4    5.0
dtype: float64

观察 s,即 。.0dtypefloat64

0赞 Corralien 2/28/2023 #2

尝试:pd.json_normalize

out = pd.concat([df, pd.json_normalize(df['XYZ'])], axis=1)
print(out)

# Output
   Id                                                XYZ Name attributes.type attributes.url
0   1  {'attributes': {'type': 'XYZ', 'url': 'somelin...  ABC             XYZ       somelink
1   2  {'attributes': {'type': 'XYZ', 'url': 'somelin...  PQR             XYZ       somelink
2   3  {'attributes': {'type': 'XYZ', 'url': 'somelin...  RST             XYZ       somelink
3   4  {'attributes': {'type': 'XYZ', 'url': 'somelin...  DEF             XYZ       somelink