Why does df.apply(tuple) work but not df.apply(list)?

六月ゝ 毕业季﹏ 提交于 2019-11-27 05:28:06

The culprit is here. With func=tuple it works, but using func=list raises an exception from within the compiled module lib.reduce:

ValueError: ('function does not reduce', 0)

As you can see, they catch the exception but don't bother to handle it.

Even without the too-broad except clause, that's a bug in pandas. You might try to raise it on their tracker, but similar issues have been closed with some flavour of wont-fix or dupe.

16321: weird behavior using apply() creating list based on current columns

15628: Dataframe.apply does not always return a Series when reduce=True

That latter issue got closed, then reopened, and converted into a docs enhancement request some months ago, and now seems to be being used as a dumping ground for any related issues.

Presumably it's not a high priority because, as piRSquared commented (and one of the pandas maintainers commented the same), you're better off with a list comprehension:

pd.Series([list(x) for x in df.itertuples(index=False)])

Typically apply would be using a numpy ufunc or similar.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!