<p><strong>使用:</strong></p>
<pre><code>m1 = df['Fault_Count'].ne(df['Fault_Count'].shift(fill_value=0))
m2 = df['Fault_Count'].eq(0) & df['Fault_Count'].shift(fill_value=0).ne(0)
s = df['Date'].groupby(m1.cumsum()).transform('first')
df['Option1'] = df['Date'].sub(s.shift()).dt.days.where(m1, 0)
df['Option2'] = df['Option1'].where(~m2, df['Option1'].mul(-1))
</code></pre>
<hr/>
<p><strong>详细信息:</strong></p>
<p>使用<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.ne.html" rel="nofollow noreferrer">^{<cd1>}</a>+<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.shift.html" rel="nofollow noreferrer">^{<cd2>}</a>创建布尔掩码<code>m1</code>,当<code>Fault_count</code>更改时表示边界条件,类似地,使用<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.eq.html" rel="nofollow noreferrer">^{<cd5>}</a>+<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.shift.html" rel="nofollow noreferrer">^{<cd2>}</a>和<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.ne.html" rel="nofollow noreferrer">^{<cd1>}</a>创建布尔掩码<code>m2</code>,表示<code>Fault_count</code>重置的条件:</p>
<pre><code> m1 m2
0 False False
1 False False
2 False False
3 True False
4 False False
5 False False
6 True False
7 False False
8 True False
9 False False
10 True True # > Fault count reset
11 True False
</code></pre>
<p>对使用<code>m1.cumsum</code>获得的连续故障计数使用<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.groupby.html" rel="nofollow noreferrer">^{<cd10>}</a>,并使用<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.core.groupby.GroupBy.first.html" rel="nofollow noreferrer">^{<cd13>}</a>转换<code>Date</code>列:</p>
<pre><code>print(s)
0 2020-01-01
1 2020-01-01
2 2020-01-01
3 2020-01-04
4 2020-01-04
5 2020-01-04
6 2020-01-07
7 2020-01-07
8 2020-01-09
9 2020-01-09
10 2020-01-11
11 2020-01-12
Name: Date, dtype: datetime64[ns]
</code></pre>
<p>使用<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.sub.html" rel="nofollow noreferrer">^{<cd14>}</a>减去使用<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.shift.html" rel="nofollow noreferrer">^{<cd2>}</a>移位的<code>s</code>的<code>Date</code>,使用<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Series.where.html" rel="nofollow noreferrer">^{<cd18>}</a>基于掩码<code>m2</code>填充<code>0</code>,并将其分配给<code>Option1</code>。类似地,我们根据掩码<code>m2</code>从<code>Option1</code>获得<code>Option2</code>:</p>
<pre><code>print(df)
Date Fault_Count Option1 Option2
0 2020-01-01 0 0.0 0.0
1 2020-01-02 0 0.0 0.0
2 2020-01-03 0 0.0 0.0
3 2020-01-04 2 3.0 3.0
4 2020-01-05 2 0.0 0.0
5 2020-01-06 2 0.0 0.0
6 2020-01-07 4 3.0 3.0
7 2020-01-08 4 0.0 0.0
8 2020-01-09 5 2.0 2.0
9 2020-01-10 5 0.0 0.0
10 2020-01-11 0 2.0 -2.0
11 2020-01-12 1 1.0 1.0
</code></pre>