pandas 数据帧索引:to_list() 与 tolist()

Mic*_*aut 8 python pandas

我最近为某人编写了一个 python 脚本,在那里我使用to_list(). 然而,这对他们不起作用,因为他们得到了:AttributeError: 'Index' object has no attribute 'to_list'使用他们的 python 解释器。

我做了一些搜索,发现tolist()似乎也有同样的事情to_list():在Pandas 文档上搜索发现两者,逐字描述相同。另一方面,Index文档只提到了to_list().

所以我想知道两者之间是否有区别

  • 在功能上
  • 受欢迎和/或“官方”/认可
  • 支持不同版本的熊猫

bur*_*ran 17

如果你查看源代码,你会看到在tolist()代码之后有一行to_list = tolist,所以to_list只是别名tolist

编辑: to_list()在版本中添加。0.24.0,见问题#8826


Mic*_*aut 5

扩展 buran 的答案(文本太多,无法放入评论中):git blame在指出的源代码上使用,可以看到它to_list()确实在 2018 年 12 月作为tolist(). 它COMMITED,以增强解决问题8826,即用,使之更加内联to_period()to_timestamp()

此外,更改和评论pandas/core/series.py表明原件tolist()是“半弃用的”:

# tolist is not actually deprecated, just suppressed in the __dir__
_deprecations = generic.NDFrame._deprecations | frozenset(
    ['asobject', 'reshape', 'get_value', 'set_value',
     'from_csv', 'valid'])
     'from_csv', 'valid', 'tolist'])
Run Code Online (Sandbox Code Playgroud)

有趣的是看看系统是如何工作的......