2012-07-11 71 views
1

我有一個Python函數historical_data,它從Yahoo Finance中提取每日曆史價格和股息數據,並將其輸出到熊貓DataFrame中。我爲什麼使用熊貓獲得is_monotonic斷言錯誤Series.asfreq

>>> nlsn = y.historical_data('NLSN') 
>>> nlsn 
<class 'pandas.core.frame.DataFrame'> 
DatetimeIndex: 366 entries, 2012-07-10 00:00:00 to 2011-01-27 00:00:00 
Data columns: 
Open   366 non-null values 
High   366 non-null values 
Low   366 non-null values 
Close  366 non-null values 
Volume  366 non-null values 
Adj Close 366 non-null values 
Dividends 366 non-null values 
dtypes: float64(6), int64(1) 
>>> nlsn['Adj Close'] 
Date 
2012-07-10 26.77 
2012-07-09 26.77 
2012-07-06 26.64 
2012-07-05 26.56 
2012-07-03 26.57 
... 
2011-02-01 25.75 
2011-01-31 26.07 
2011-01-28 25.00 
2011-01-27 25.40 
Name: Adj Close, Length: 366 

我只想持續存儲每日數據(而不必存儲每日,每月,每週等)。每天到每月轉換似乎不以下工作,雖然:

>>> nlsn['Adj Close'].asfreq('M', method='bfill') 
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "/home/michael/Projects/envs/fintools32/lib/python3.2/site-packages/pandas/core/generic.py", line 156, in asfreq 
    return asfreq(self, freq, method=method, how=how) 
    File "/home/michael/Projects/envs/fintools32/lib/python3.2/site-packages/pandas/tseries/resample.py", line 329, in asfreq 
    return obj.reindex(dti, method=method) 
    File "/home/michael/Projects/envs/fintools32/lib/python3.2/site-packages/pandas/core/series.py", line 2053, in reindex 
    level=level, limit=limit) 
    File "/home/michael/Projects/envs/fintools32/lib/python3.2/site-packages/pandas/core/index.py", line 791, in reindex 
    limit=limit) 
    File "/home/michael/Projects/envs/fintools32/lib/python3.2/site-packages/pandas/core/index.py", line 719, in get_indexer 
    assert(self.is_monotonic) 
AssertionError 

什麼是適合我的方式聚合這些股票的價格爲每月?

我已經試過

我嘗試了所有不同method參數(ffill,墊,bfill),所有這些似乎都提出同樣的斷言錯誤。

我試圖檢查源代碼index.py,但似乎是在效果策略模式,其中有問題的代表is_monotonic類其_engine屬性,我找不到在_engine屬性實際分配。

回答

2

嘗試nlsn['Adj Close'][::-1].asfreq('M', method='ffill')

如果你能得到你的函數返回升序DatetimeIndex,它會讓你跳過額外的分揀這裏。

+0

工作。在返回之前,我將原始函數更改爲sort_index(inplace = True)DataFrame。 – MikeRand 2012-07-11 15:59:56

相關問題