<p>很抱歉带回一个旧的帖子,但我注意到罗曼的回答有点不对劲——计数不正确,导致结果不准确。计数列中应该有4项:[2,4,1,2],最大值为4。在</p>
<p>为了证明这个问题-我已经把它分解了一点(df与上面接受的答案相同)。您可以看到结果组不正确:</p>
<pre><code># sort
dfS = df.sort_index(inplace=True)
# reset
dfSR = dfS.reset_index(inplace=True)
# group
dfG = dfSR.groupby(df['A'].cumsum())
# show resulting groups
for group in dfG: print(group)
# (1, index A
# 0 2015-05-01 True)
# (2, index A
# 1 2015-05-02 True
# 2 2015-05-03 False
# 3 2015-05-04 False
# 4 2015-05-05 False
# 5 2015-05-06 False)
# (3, index A
# 6 2015-05-07 True
# 7 2015-05-08 False
# 8 2015-05-09 False)
</code></pre>
<p>多亏了DSM <a href="https://stackoverflow.com/questions/27626542/counting-consecutive-positive-value-in-python-array">here</a>的回答,当然还有罗曼的回答,将这两篇文章的技术结合起来就得到了答案。它们已经在它们来自的帖子中进行了解释,所以我将在下面的代码中继续讨论。在</p>
^{pr2}$
<p>输出:</p>
<pre><code>(1, index A
0 2015-05-01 True
1 2015-05-02 True)
(2, index A
2 2015-05-03 False
3 2015-05-04 False
4 2015-05-05 False
5 2015-05-06 False)
(3, index A
6 2015-05-07 True)
(4, index A
7 2015-05-08 False
8 2015-05-09 False)
count min max
A
1 2 2015-05-01 2015-05-02
2 4 2015-05-03 2015-05-06
3 1 2015-05-07 2015-05-07
4 2 2015-05-08 2015-05-09
count min max
A
2 4 2015-05-03 2015-05-06
</code></pre>