2016-02-24 22 views
2

這段代碼的目的是在每一天的每一分鐘得到最近3天數據的平均值。如何使用熊貓來計算x天的分鐘數據的平均/標準值?

如果今天是2016年1月3日,我想知道的平均收盤值的最後3天09:30:00包括今天,那麼僞公式會像下面:

3 - 今日平均收市時間爲今日09:30:00 = (收盤價於2016-01-01 09:30:00 +收盤價於2016-01-02 09:30:00 +收盤價於2016-01- 03 09:30:00)/ 3

我設法使用熊貓來計算x天的分鐘數據的平均/標準值。 下面的代碼是我實現的。

import pandas as pd 
import numpy as np 

# date, time, close 
data = [ 
    [20150101, 90100, 100], 
    [20150101, 90200, 102], 
    [20150101, 90300, 104], 
    [20150101, 90400, 106], 
    [20150101, 90500, 108], 

    [20150102, 90100, 100], 
    [20150102, 90200, 104], 
    [20150102, 90300, 105], 
    [20150102, 90400, 103], 
    [20150102, 90500, 102], 

    [20150103, 90100, 100], 
    [20150103, 90200, 98], 
    [20150103, 90300, 99], 
    [20150103, 90400, 102], 
    [20150103, 90500, 101], 

    [20150104, 90100, 100], 
    [20150104, 90200, 101], 
    [20150104, 90300, 100], 
    [20150104, 90400, 100], 
    [20150104, 90500, 101], 

    [20150105, 90100, 100], 
    [20150105, 90200, 102], 
    [20150105, 90300, 104], 
    [20150105, 90400, 106], 
    [20150105, 90500, 108], 
] 

df = pd.DataFrame(data, columns = ['date', 'time', 'close']) 
df.set_index(['date', 'time'], inplace=True) 

################################################################ 

df.groupby(level=0) 
dateidx = sorted(list(set(date for (date, time) in df.index))) 
timeidx = sorted(list(set(time for (date, time) in df.index))) 
print(dateidx) 
print(timeidx) 

df['mean'] = np.nan 
df['std'] = np.nan 

print(df) 

idx = len(timeidx)*2 
for i in range(5-2): 
    slice=df.loc[dateidx[i]:dateidx[i+2]] 
    times = slice.groupby(level='time') 
    means = times.mean() 
    stds = times.std() 
    print('[means]') 
    print(means) 

    for i in range(len(timeidx)): 
     df['mean'].iloc[idx] = means['close'].iloc[i] 
     df['std'].iloc[idx] = stds['close'].iloc[i] 
     idx = idx + 1 

print(df)  

以下是最終結果。

   close  mean  std 
date  time 
20150101 90100 100   NaN  NaN 
     90200 102   NaN  NaN 
     90300 104   NaN  NaN 
     90400 106   NaN  NaN 
     90500 108   NaN  NaN 
20150102 90100 100   NaN  NaN 
     90200 104   NaN  NaN 
     90300 105   NaN  NaN 
     90400 103   NaN  NaN 
     90500 102   NaN  NaN 
20150103 90100 100 100.000000 0.000000 
     90200  98 101.333333 3.055050 
     90300  99 102.666667 3.214550 
     90400 102 103.666667 2.081666 
     90500 101 103.666667 3.785939 
20150104 90100 100 100.000000 0.000000 
     90200 101 101.000000 3.000000 
     90300 100 101.333333 3.214550 
     90400 100 101.666667 1.527525 
     90500 101 101.333333 0.577350 
20150105 90100 100 100.000000 0.000000 
     90200 102 100.333333 2.081666 
     90300 104 101.000000 2.645751 
     90400 106 102.666667 3.055050 
     90500 108 103.333333 4.041452 

但問題是,上面的代碼太慢,有點複雜。 那麼,有沒有人建議這個問題的最佳代碼或解決方案?

ps。我想刪除那些用於計算要更改的行的位置的常量。請建議更簡單和優雅的方式。

+0

您能否介紹一下您正在努力實現的目標和您的預期成果? – Alexander

+0

@Alexander其他評論已更新。 – user1913171

回答

0

我相信你會發現這個自我解釋。

df = pd.DataFrame(data, columns = ['date', 'time', 'close']).set_index(['date', 'time']) 

df['mean'] = df.groupby(level='time')['close'].apply(lambda x: pd.rolling_mean(x, window=3)) 
df['std'] = df.groupby(level='time')['close'].apply(lambda x: pd.rolling_std(x, window=3)) 

>>> df 
       close  mean  std 
date  time        
20150101 90100 100   NaN  NaN 
     90200 102   NaN  NaN 
     90300 104   NaN  NaN 
     90400 106   NaN  NaN 
     90500 108   NaN  NaN 
20150102 90100 100   NaN  NaN 
     90200 104   NaN  NaN 
     90300 105   NaN  NaN 
     90400 103   NaN  NaN 
     90500 102   NaN  NaN 
20150103 90100 100 100.000000 0.000000 
     90200  98 101.333333 3.055050 
     90300  99 102.666667 3.214550 
     90400 102 103.666667 2.081666 
     90500 101 103.666667 3.785939 
20150104 90100 100 100.000000 0.000000 
     90200 101 101.000000 3.000000 
     90300 100 101.333333 3.214550 
     90400 100 101.666667 1.527525 
     90500 101 101.333333 0.577350 
20150105 90100 100 100.000000 0.000000 
     90200 102 100.333333 2.081666 
     90300 104 101.000000 2.645751 
     90400 106 102.666667 3.055050 
     90500 108 103.333333 4.041452 
+0

謝謝你的代碼。但是,我的df已經有'日期'和'時間'列作爲索引。而你的代碼假設'日期'和'時間'列是正常的數據列。 df有'date'和'time'索引的另一種方法嗎? – user1913171

+0

我感謝您的幫助。再次感謝你 :) – user1913171

相關問題