2016-11-11 126 views
9

我有熊貓系列形式[0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0 , 0 , 1].熊貓:標誌連續值

0: indicates economic increase. 
1: indicates economic decline. 

甲衰退是由兩個連續的下降用信號通知(1)。

經濟衰退的結束是通過連續兩次增加(0)來標誌的。

在上述數據集I有兩個衰退,開始於索引3,端在索引5和索引8端開始於索引11.

我在一個丟失如何與熊貓接近這個。我想確定經濟衰退開始和結束的指數。任何援助將不勝感激。

這是我的蟒蛇嘗試在soln。

np_decline = np.array([0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0 , 0 , 1]) 
recession_start_flag = 0 
recession_end_flag = 0 
recession_start = [] 
recession_end = [] 

for i in range(len(np_decline) - 1): 
    if recession_start_flag == 0 and np_decline[i] == 1 and np_decline[i + 1] == 1: 
     recession_start.append(i) 
     recession_start_flag = 1 
    if recession_start_flag == 1 and np_decline[i] == 0 and np_decline[i + 1] == 0: 
     recession_end.append(i - 1) 
     recession_start_flag = 0 

print(recession_start) 
print(recession_end) 

更多以熊貓爲中心的方法嗎? 萊昂

回答

3

運行的開始1的滿足條件

x_prev = x.shift(1) 
x_next = x.shift(-1) 
((x_prev != 1) & (x == 1) & (x_next == 1)) 

也就是說,在運行開始時的值是1並且以前的值不爲1,下一個值是1。類似地,運行結束滿足條件

((x == 1) & (x_next == 0) & (x_next2 == 0)) 

由於運行結束時的值爲1,接下來的兩個值爲0。 我們可以使用np.flatnonzero尋找到滿足這些條件指數:

import numpy as np 
import pandas as pd 

x = pd.Series([0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0 , 0 , 1]) 
x_prev = x.shift(1) 
x_next = x.shift(-1) 
x_next2 = x.shift(-2) 
df = pd.DataFrame(
    dict(start = np.flatnonzero((x_prev != 1) & (x == 1) & (x_next == 1)), 
     end = np.flatnonzero((x == 1) & (x_next == 0) & (x_next2 == 0)))) 
print(df[['start', 'end']]) 

產生

start end 
0  3 5 
1  8 11 
4

您可以使用shift

df = pd.DataFrame([0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0 , 0 , 1], columns=['signal']) 
df_prev = df.shift(1)['signal'] 
df_next = df.shift(-1)['signal'] 
df_next2 = df.shift(-2)['signal'] 
df.loc[(df_prev != 1) & (df['signal'] == 1) & (df_next == 1), 'start'] = 1 
df.loc[(df['signal'] != 0) & (df_next == 0) & (df_next2 == 0), 'end'] = 1 
df.fillna(0, inplace=True) 
df = df.astype(int) 

    signal start end 
0  0  0 0 
1  1  0 0 
2  0  0 0 
3  1  1 0 
4  1  0 0 
5  1  0 1 
6  0  0 0 
7  0  0 0 
8  1  1 0 
9  1  0 0 
10  0  0 0 
11  1  0 1 
12  0  0 0 
13  0  0 0 
14  1  0 0 
+1

或許只是'.fillna' 0使它乾淨利落。 –

+0

@ juanpa.arrivillaga謝謝,更新 –

4

使用rolling(2)

s = pd.Series([0, 1, 0, 1, 1, 1, 0, 0, 1, 1, 0, 1, 0 , 0 , 1]) 

我減去.5所以rolling總和爲1當經濟衰退開始,-1當它停止。

s2 = s.sub(.5).rolling(2).sum() 

因爲兩者1-1評估爲True我可以掩蓋滾動信號,剛開始和停止,ffill。通過gt(0)獲得正面或負面的真實值。

pd.concat([s, s2.mask(~s2.astype(bool)).ffill().gt(0)], axis=1, keys=['signal', 'isRec']) 

enter image description here

+0

Hi @piRSquared。我怎麼能修改你已經完成了兩列經濟衰退開始和經濟衰退結束。如果後退開始將是真的,如果它的衰退開始否則爲假,當然如果衰退已結束爲真否則結束是否則爲假。 –

4

使用shift,但是寫的結果作爲一個單一的布爾列類似的想法:

# Boolean indexers for recession start and stops. 
rec_start = (df['signal'] == 1) & (df['signal'].shift(-1) == 1) 
rec_end = (df['signal'] == 0) & (df['signal'].shift(-1) == 0) 

# Mark the recession start/stops as True/False. 
df.loc[rec_start, 'recession'] = True 
df.loc[rec_end, 'recession'] = False 

# Forward fill the recession column with the last known Boolean. 
# Fill any NaN's as False (i.e. locations before the first start/stop). 
df['recession'] = df['recession'].ffill().fillna(False) 

輸出結果:

signal recession 
0  0  False 
1  1  False 
2  0  False 
3  1  True 
4  1  True 
5  1  True 
6  0  False 
7  0  False 
8  1  True 
9  1  True 
10  0  True 
11  1  True 
12  0  False 
13  0  False 
14  1  False