Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

python - Why doesn't my pandas rolling().apply() work when the series contains collections?

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

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

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

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

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...