本文介绍了当该系列包含集合时,为什么我的 pandas rolling().apply()不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个熊猫系列,其中每个单元都是一个元组.我正在尝试对该系列进行rolling().apply(),而我尝试应用的函数从未被调用.这是一个愚蠢的例子,展示了我在说什么:

I've got a pandas series in which each cell is a tuple. I'm trying to do a rolling().apply() on that series, and the function I'm trying to apply is never getting called. Here's a silly example that shows what I'm talking about:

>>> import pandas as pd
>>> pd.__version__
u'0.18.0'
>>> die = lambda x: 0/0

>>> s = pd.Series(zip(range(5), range(5)))
>>> s
0    (0, 0)
1    (1, 1)
2    (2, 2)
3    (3, 3)
4    (4, 4)
dtype: object

一个简单的apply可以按预期工作,因为该函数称为:

A simple apply works as expected, in that the function is called:

>>> s.apply(die)
[...]
ZeroDivisionError: integer division or modulo by zero

但是rolling().apply()却什么也不做,特别是应该被调用的函数永远不会被调用:

But but a rolling().apply() does nothing at all, and in particular the function that is supposed to be applied never gets called:

>>> s.rolling(2).apply(die)
0    (0, 0)
1    (1, 1)
2    (2, 2)
3    (3, 3)
4    (4, 4)
dtype: object

这是演示我在说什么的最简单的示例,但是set&列表.

This is the simplest example that demonstrates what I'm talking about, but the same thing happens with sets & lists.

为什么会发生这种情况,以及如何使用自定义函数对一系列集合进行滚动应用?

Why does this happen, and how can I do a rolling apply with a custom function on a series of collections?

推荐答案

这将无效,因为会返回为特定操作子类化的Window或Rolling子类,而 pandas.DataFrame.apply 沿DataFrame的输入轴应用功能.如ayhan所述,在帖子中.

This will not work because the pandas.DataFrame.rolling function returns a Window or Rolling sub-classed for the particular operation while pandas.DataFrame.apply Applies function along input axis of DataFrame. As mentioned by ayhan, in this post.

这篇关于当该系列包含集合时,为什么我的 pandas rolling().apply()不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-01 22:26