2013-07-22 143 views
4

我正在讀取多個時間序列的電子表格到一個熊貓數據框中,並將它們與一個普通的熊貓日期時間索引連接在一起。記錄時間序列的數據記錄器不是100%準確的,這使得重採樣非常煩人,因爲根據時間稍微高於還是低於採樣間隔,它將創建NaN並開始使我的系列看起來像一條虛線。這裏是我的代碼圓熊貓日期時間索引?

def loaddata(filepaths): 
    t1 = time.clock() 
    for i in range(len(filepaths)): 
     xl = pd.ExcelFile(filepaths[i]) 
     df = xl.parse(xl.sheet_names[0], header=0, index_col=2, skiprows=[0,2,3,4], parse_dates=True) 
     df = df.dropna(axis=1, how='all') 
     df = df.drop(['Decimal Year Day', 'Decimal Year Day.1', 'RECORD'], axis=1) 

     if i == 0: 
      dfs = df 
     else: 
      dfs = concat([dfs, df], axis=1) 
    t2 = time.clock() 
    print "Files loaded into dataframe in %s seconds" %(t2-t1) 

files = ["London Lysimeters corrected 5min.xlsx", "London Water Balance 5min.xlsx"] 
data = loaddata(files) 

這裏的指數的一個想法:

data.index

類的pandas.tseries.index.DatetimeIndex'> [2012年8月27日12: 05:00.000002,......,2013年7月12日15:10:00.000004] 長度:91910,頻率:無,時區:無

什麼是最快和最一般將指數四捨五入到最接近的分鐘?

回答

6

這是一個小技巧。日期時間爲納秒(當被視爲np.int64時)。 012-這麼簡單,以毫微秒爲單位。

In [75]: index = pd.DatetimeIndex([ Timestamp('20120827 12:05:00.002'), Timestamp('20130101 12:05:01'), Timestamp('20130712 15:10:00'), Timestamp('20130712 15:10:00.000004') ]) 

In [79]: index.values 
Out[79]: 
array(['2012-08-27T08:05:00.002000000-0400', 
     '2013-01-01T07:05:01.000000000-0500', 
     '2013-07-12T11:10:00.000000000-0400', 
     '2013-07-12T11:10:00.000004000-0400'], dtype='datetime64[ns]') 

In [78]: pd.DatetimeIndex(((index.asi8/(1e9*60)).round()*1e9*60).astype(np.int64)).values 
Out[78]: 
array(['2012-08-27T08:05:00.000000000-0400', 
     '2013-01-01T07:05:00.000000000-0500', 
     '2013-07-12T11:10:00.000000000-0400', 
     '2013-07-12T11:10:00.000000000-0400'], dtype='datetime64[ns]') 
+0

作品完美,速度非常快!我不知道你可以用這種方式處理datetimeindex – pbreach

+0

你實際上*有*處理它們的方式(這個''index''大部分隱藏) – Jeff

+3

這也可能是一個很好的補充索引代碼,你可以按照這個問題:https://github.com/pydata/pandas/issues/4314 – Jeff

0

對於數據列; 使用:由Jeff提到round_hour(df.Start_time)

def round_hour(x,tt=''): 
    if tt=='M': 
     return pd.to_datetime(((x.astype('i8')/(1e9*60)).round()*1e9*60).astype(np.int64)) 
    elif tt=='H': 
     return pd.to_datetime(((x.astype('i8')/(1e9*60*60)).round()*1e9*60*60).astype(np.int64)) 
    else: 
     return pd.to_datetime(((x.astype('i8')/(1e9)).round()*1e9).astype(np.int64)) 
4

問題4314現已關閉並在大熊貓0.18.0加入用於DatetimeIndex,時間戳,和TimedeltaIndex Timedelta round()方法。現在,我們可以做到以下幾點:

In[109]: index = pd.DatetimeIndex([pd.Timestamp('20120827 12:05:00.002'), pd.Timestamp('20130101 12:05:01'), pd.Timestamp('20130712 15:10:30'), pd.Timestamp('20130712 15:10:31')]) 

In[110]: index.values 
Out[110]: 
array(['2012-08-27T12:05:00.002000000', '2013-01-01T12:05:01.000000000', 
     '2013-07-12T15:10:30.000000000', '2013-07-12T15:10:31.000000000'], dtype='datetime64[ns]') 

In[111]: index.round('min') 
Out[111]: 
DatetimeIndex(['2012-08-27 12:05:00', '2013-01-01 12:05:00', 
       '2013-07-12 15:10:00', '2013-07-12 15:11:00'], 
       dtype='datetime64[ns]', freq=None) 

round()接受頻率參數。它的字符串別名列於here

+0

是的,'round()'解決了它! –

相關問題