Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python BaseIndexer仍与.count()和.min()一起损坏?_Python_Pandas_Rolling Computation - Fatal编程技术网

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)
用作解决方法