2016-10-09 153 views
0

我想重新採樣熊貓系列之間的時間差距問題重新採樣大熊貓時間序列指數

import pandas as pd 
index_1 = pd.date_range('1/1/2000', periods=4, freq='T') 
index_2 = pd.date_range('1/2/2000', periods=3, freq='T') 
series = pd.Series(range(4), index=index_1) 
series=series.append(pd.Series(range(3), index=index_2)) 
print series 
>>>2000-01-01 00:00:00 0 
    2000-01-01 00:01:00 1 
    2000-01-01 00:02:00 2 
    2000-01-01 00:03:00 3 
    2000-01-02 00:00:00 0 
    2000-01-02 00:01:00 1 
    2000-01-02 00:02:00 2 

,使得所得DataSeries只包含每一第二項,即

>>>2000-01-01 00:00:00 0 
    2000-01-01 00:02:00 2 
    2000-01-02 00:00:00 0 
    2000-01-02 00:02:00 2 

使用(不佳記錄)熊貓的重採樣方法如下:

resampled_series = series.resample('2T', closed='right') 
print resampled_series 

我得到

>>>1999-12-31 23:58:00 0.0 
    2000-01-01 00:00:00 1.5 
    2000-01-01 00:02:00 3.0 
    2000-01-01 00:04:00 NaN 
    2000-01-01 00:56:00 NaN 
        ... 
    2000-01-01 23:54:00 NaN 
    2000-01-01 23:56:00 NaN 
    2000-01-01 23:58:00 0.0 
    2000-01-02 00:00:00 1.5 
    2000-01-02 00:02:00 3.0 

爲什麼它比原始系列提前2分鐘?爲什麼它包含了原始序列中沒有包含的所有時間步驟?我怎樣才能得到我想要的結果?

回答

1

resample()不適合您的目的。

試試這個:

series[series.index.minute % 2 == 0] 
+0

謝謝您的回答。但是,resample實際上是什麼? http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.resample.html沒有給出非常全面的答案。有幾個有用的例子,但大多數參數和它們的可能值沒有很好解釋。 – user1934212

+0

以下是文檔:http://pandas.pydata.org/pandas-docs/stable/timeseries.html#sparse-resampling – HYRY