Python BaseIndexer仍与.count()和.min()一起损坏?
熊猫中的一种动物。这适用于Python BaseIndexer仍与.count()和.min()一起损坏?,python,pandas,rolling-computation,Python,Pandas,Rolling Computation,熊猫中的一种动物。这适用于.mean()和.sum()等情况。据我所知,像.count()和.min()这样的聚合过去是很常见的。当前.count()使用内部roll\u count函数AFAICS。但我仍然没有得到预期的结果: import numpy as np import pandas as pd # Use largest most recent multiple of *modulo* past measurements: class ModuloIndexer(pd.api.in
.mean()
和.sum()
等情况。据我所知,像.count()
和.min()
这样的聚合过去是很常见的。当前.count()
使用内部roll\u count
函数AFAICS。但我仍然没有得到预期的结果:
import numpy as np
import pandas as pd
# Use largest most recent multiple of *modulo* past measurements:
class ModuloIndexer(pd.api.indexers.BaseIndexer):
def get_window_bounds(self, num_values, min_periods, center, closed):
end = np.arange(1, num_values + 1, dtype=np.int64)
start = end % self.modulo
return start, end
s = pd.Series(2 ** np.arange(8)) # [1, 2, 4, 8, 16, 32, 64, 128]
r = s.rolling(ModuloIndexer(s.index, modulo=4))
print(r.sum()) # Correct: [0, 0, 0, 15, 30, 60, 120, 255]
print(r.apply(len)) # Correct: [0, 0, 0, 4, 4, 4, 4, 8]
print(r.count()) # Weird: [nan, nan, nan, 1, 1, 1, 1, 2]
print(r.apply(np.min)) # Correct: [nan, nan, nan, 1, 2, 4, 8, 1]
print(r.min()) # Weird: [nan, nan, nan, 8, 8, 8, 8, 8]
我是做错了什么,还是这是一个我应该报告的错误
注:仅当不存在nan
s时,才将apply(len)
用作解决方法