2013-01-02 25 views
1

我有日期,這樣索引的數據幀:如何索引與「孔」有TimeSeries的一個數據幀無孔

<class 'pandas.core.frame.DataFrame'> 
DatetimeIndex: 1853141 entries, 2012-03-01 00:00:00 to 2012-06-16 23:59:55 
Data columns: 
Open Bid ESM2 1853141 non-null values 
Open Ask ESM2 1853141 non-null values 
dtypes: float64(2) 

指數有一些「洞」了一段5秒,所以我創建具有相同的日期範圍和週期的TimeSeries無孔:

<class 'pandas.tseries.index.DatetimeIndex'> 
[2012-03-01 00:00:00, ..., 2012-06-16 23:59:55] 
Length: 1866240, Freq: 5S, Timezone: None 

現在我想利用這段時間系列作爲指標上面列出楠洞的數據幀。我該怎麼做

回答

4

您可以使用reindex()方法並傳遞您創建的填充索引。
默認fill_valueNaN

In [1]: ix1 = [0, 1, 2, 3, 4, 9] 

In [2]: ix1 
Out[2]: [0, 1, 2, 3, 4, 9] 

In [3]: ix2 = range(10) 

In [4]: ix2 
Out[4]: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] 

In [5]: s = Series(ix1, index=ix1) 

In [6]: s 
Out[6]: 
0 0 
1 1 
2 2 
3 3 
4 4 
9 9 

In [7]: s.reindex(ix2) 
Out[7]: 
0  0 
1  1 
2  2 
3  3 
4  4 
5 NaN 
6 NaN 
7 NaN 
8 NaN 
9  9 

In [8]: Series.reindex()? 

Docstring: 
Conform Series to new index with optional filling logic, placing 
NA/NaN in locations having no value in the previous index. A new object 
is produced unless the new index is equivalent to the current one and 
copy=False