Pandas 弃用警告:to_dict()函数。

11

我收到了这个已弃用警告:

Using short name for 'orient' is deprecated. Only the options: ('dict', list, 'series', 'split', 'records', 'index') will be used in a future version. Use one of the above to silence this warning.

当使用这些行时:

df.to_dict('records')
df.to_dict(orient='records')
df.to_dict(orientation='records')

pandas v1.1.3 python v3.7.1


第一和第二个引发错误了吗? - jezrael
1
源代码 和警告中,我预计 'records' 不会触发此警告。另外,我想知道为什么 orientation=... 在这里是一个有效的关键字参数。在不运行代码的情况下,我预计你的第三行会出错。 - Niklas Mertsch
@Amir(抱歉)问题是为什么会出现警告。就我所理解的警告而言,我不应该使用df.to_dict(orient='records'),而应该使用我所做的df.dict('records')。 - Luggie Böörgöör
@NiklasMertsch 是的,第三行没有收到错误信息让我感到困惑。 - Luggie Böörgöör
2
我在我的安装中得到了预期的结果:orientation 抛出一个错误,'records'orient='records' 正常工作,'orient='r'' 引起了所描述的警告并且也正常工作。 - Niklas Mertsch
3个回答

8

警告提示"orient"已被废弃。请按照以下方式使用:

df.to_dict('records')

不要再使用orient='',而是直接使用其中任何一种('dict', list, 'series', 'split', 'records', 'index'),如:

df.to_dict('dict')

df.to_dict('list')


9
警告信息将会是我所假设的内容。但是df.to_dict('records')仍然会引发警告。 - Luggie Böörgöör

5

我曾遇到同样的警告而感到困惑。后来发现我使用了 "record" 而不是 "records"。无论如何,您可以在警告之前的大约1485行处,在文件“pandas/core/frame.py”中插入 print(orient) 行。


“records” 不适用于我。 - Luggie Böörgöör

1
当我使用这行代码时,出现了相同的警告:df.to_dict('records') 但是当我尝试 df.to_dict(orient = 'records') 时,警告消失了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接