2017-05-26 51 views
1

下面是SQL查詢,在新的格式更新日期Python的數據幀列的更新,更換爲每where條件幫助需要

update data set Date=[Time Period]+'-01-01' where Frequency='0' 

update data set Date=replace([Time Period],'Q1','-01-01') 
where Frequency='2' and substring([Time Period],5,2)='Q1' 

update data set Date=replace([Time Period],'Q2','-04-01') 
where Frequency='2' and substring([Time Period],5,2)='Q2' 

update data set Date=replace([Time Period],'Q3','-07-01') 
where Frequency='2' and substring([Time Period],5,2)='Q3' 

update data set Date=replace([Time Period],'Q4','-10-01') 
where Frequency='2' and substring([Time Period],5,2)='Q4' 

update data set Date=replace([Time Period],'M','-')+'-01' 
where Frequency='3' and len([Time Period])=7 

update data set Date=replace([Time Period],'M','-0')+'-01' 
where Frequency='3' and len([Time Period])=6 

現在我已經加載相同的數據到Python數據幀,從

的樣本數據數據框用逗號分隔。 列:時間段是輸入數據和日期列是輸出日期,我需要將時間段轉換爲列日期格式。

Frequency,Time Period,Date 
0,2008,2008-01-01 
0,1961,1961-01-01 
2,2009Q1,2009-04-01 
2,1975Q4,1975-10-01 
2,2007Q3,2007-04-01 
2,1959Q4,1959-10-01 
2,1965Q4,1965-07-01 
2,2008Q3,2008-07-01 
3,1969M2,1969-02-01 
3,1994M12,1994-12-01 
3,1990M1,1990-01-01 
3,1994M10,1994-10-01 
3,2012M11,2012-11-01 
3,1994M3,1994-03-01 

請讓我知道如何更新日期根據上述條件在Python中。

+1

能否請您發佈樣本_input_和_output_數據集(5-7行的CSV /字典/ JSON/Python代碼格式__as text__,所以編碼。一個人可以使用它)並描述你想用輸入數據做什麼以獲得輸出數據集? [如何創建一個最小,完整和可驗證的示例](http://stackoverflow.com/help/mcve) – MaxU

+0

最大,我已添加示例數據,請檢查並幫助此... –

+0

現在好多了!請同時發佈__desired__數據集。或者它是「日期」欄 - 它已經是你想要的結果了嗎? – MaxU

回答

0

當添加不同的偏移量時,使用矢量化apparoach有點棘手。

考慮以下方法:

來源DF:

In [337]: df 
Out[337]: 
    Frequency Time Period 
0   0  2008 
1   0  1961 
2   2  2009Q1 
3   2  1975Q4 
4   2  2007Q3 
5   2  1959Q4 
6   2  1965Q4 
7   2  2008Q3 
8   3  1969M2 
9   3  1994M12 
10   3  1990M1 
11   3  1994M10 
12   3  2012M11 
13   3  1994M3 

解決方案:

In [338]: %paste 
df[['y','mm']] = (df['Time Period'] 
        .replace(['Q1', 'Q2', 'Q3', 'Q4'], 
          ['M0', 'M3', 'M6', 'M9'], 
          regex=True) 
        .str.extract('(\d{4})M?(\d+)?', expand=True)) 

df['Date'] = (pd.to_datetime(df.pop('y'), format='%Y', errors='coerce') 
       .values.astype('M8[M]') \ 
       + \ 
       pd.to_numeric(df.pop('mm'), errors='coerce') \ 
       .fillna(0).astype(int).values * np.timedelta64(1, 'M')) \ 
      .astype('M8[D]') 
## -- End pasted text -- 

結果:

In [339]: df 
Out[339]: 
    Frequency Time Period  Date 
0   0  2008 2008-01-01 
1   0  1961 1961-01-01 
2   2  2009Q1 2009-01-01 
3   2  1975Q4 1975-10-01 
4   2  2007Q3 2007-07-01 
5   2  1959Q4 1959-10-01 
6   2  1965Q4 1965-10-01 
7   2  2008Q3 2008-07-01 
8   3  1969M2 1969-03-01 
9   3  1994M12 1995-01-01 
10   3  1990M1 1990-02-01 
11   3  1994M10 1994-11-01 
12   3  2012M11 2012-12-01 
13   3  1994M3 1994-04-01 

編輯斯科特波士頓請刪除,如果你找到一個更好的辦法。

df[['y','mm']] = (df['Period'] 
        .replace(['Q1', 'Q2', 'Q3', 'Q4'], 
          ['M1', 'M4', 'M7', 'M10'], 
          regex=True) 
        .str.extract('(\d{4})M?(\d+)?', expand=True)) 

df['Date'] = (pd.to_datetime(df.pop('y'), format='%Y', errors='coerce') 
       .values.astype('M8[M]') \ 
       + \ 
       pd.to_numeric(df.pop('mm'), errors='coerce') \ 
       .fillna(1).astype(int).values - 1 * np.timedelta64(1, 'M')) \ 
      .astype('M8[D]') 

輸出:

Frequency Time Period  Date 
0   0  0  2008 2008-01-01 
1   1  0  1961 1961-01-01 
2   2  2 2009Q1 2009-01-01 
3   3  2 1975Q4 1975-10-01 
4   4  2 2007Q3 2007-07-01 
5   5  2 1959Q4 1959-10-01 
6   6  2 1965Q4 1965-10-01 
7   7  2 2008Q3 2008-07-01 
8   8  3 1969M2 1969-02-01 
9   9  3 1994M12 1994-12-01 
10   10  3 1990M1 1990-01-01 
11   11  3 1994M10 1994-10-01 
12   12  3 2012M11 2012-11-01 
13   13  3 1994M3 1994-03-01 
+0

超@Max,只棘手,將檢查並更新您...非常感謝... –

+0

@Linus,請考慮[接受](http ://meta.stackexchange.com/a/5235)答案如果你認爲它已經回答了你的問題 – MaxU

+0

時間段:1969M2,1994M12,1990M1,1994M10,2012M11,1994M3,給出了錯誤的結果......你能不能更新修訂腳本。 –