熊猫选择自定义月末日期

时间:2014-11-30 17:43:40

标签: python pandas

我一直在玩月末的不同定义,包括实际上一个日历日之前和之后的天数。熊猫有一些很好的工具,允许选择月末和使用shift()方法我能够在该月的实际最后一天之前和之后的几天移动日期。

我苦苦挣扎的是选择每月间隔排除那些日子在月末的日子,并最终写出dict理解的组合并调用pd.Series。我确信有更好的方法来做我做的事情,我很乐意找到它。以下是供参考的代码:

%matplotlib inline
import seaborn as sns
import pandas as pd
from datetime import datetime
from pandas.io.data import DataReader

start_date = '2012-09-09'
end_date = datetime.today()
SPX = DataReader('^gspc', 'yahoo', start_date, end_date)

#Select the month end straddle interval
back, fwd = -3, 4
me_range = abs(back) + fwd 

#Calculate dates corresponding to the month end interval
dt = [pd.date_range(start_date, end_date, freq='BM').shift(i, freq='B') 
      for i in range(back,fwd)]
#Filter time series for rest of the month values 
rest_of_month_data = SPX.Close[SPX.index-dt[0].append([dt[i] for i in
                     range(1,len(dt))])]
#Extract values for the last day of the month before the beginning of month end range
last_day  = pd.Series({rest_of_month_data.index[i-1] : rest_of_month_data[i-1]
                   for i in range(1,len(rest_of_month_data))
                   if rest_of_month_data.index.month[i] !=    
                   rest_of_month_data.index.month[i-1]})
last_day  = last_day.append(pd.Series({rest_of_month_data.index[-1] :   
                                       rest_of_month_data[-1]}))

#Extract values for the beginning of the month after month end range
first_day = pd.Series({rest_of_month_data.index[j] : rest_of_month_data[j] 
                       for j in range(len(rest_of_month_data)-1)
                       if rest_of_month_data.index.month[j] != 
                       rest_of_month_data.index.month[j-1]})
 #Calculate returns
 rest_of_month_rets = pd.Series({last_day.index[i]: 1 + (last_day.values[i] -   
                                 first_day.values[i]) / 
                                 first_day.values[i]
                                 for i in range(len(last_day))}).cumprod()
 #Plot results
 (rest_of_month_rets - 1).plot(title = 'Rest of Month Strategy Returns');

我试图通过不属于dt的值(我分配给月末范围的变量)来索引我的系列,但由于我保留了AttributeError,我无法出于某种原因:'NotImplementedType'当我试图运行时,对象没有属性'dtype':

ES.Open[ES.index!=dt[0].append([dt[i] for i in range(1,len(dt))])]

选择所需的日期。这就是我使用之前编写的函数的原因。为完整起见,这是完整的错误消息。

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-38-aa2d213a3d00> in <module>()
----> 1 ES.Open[ES.index!=dt[0].append([dt[i] for i in range(1,len(dt))])]

/usr/local/lib/python3.4/dist-packages/pandas-0.15.1_91_g0a2ea0a-py3.4-
linux-x86_64.egg /pandas/tseries/index.py in wrapper(self, other)
 79             elif not isinstance(other, (np.ndarray, Index, ABCSeries)):
 80                 other = _ensure_datetime64(other)
 ---> 81             result = func(np.asarray(other))
 82             result = _values_from_object(result)
 83 

 /usr/local/lib/python3.4/dist-packages/pandas-0.15.1_91_g0a2ea0a-py3.4-linux-
 x86_64.egg/pandas/core/index.py in wrapper(self, other)
 55         # technically we could support bool dtyped Index
 56         # for now just return the indexing array directly
 ---> 57         if com.is_bool_dtype(result):
 58             return result
 59         try:

 /usr/local/lib/python3.4/dist-packages/pandas-0.15.1_91_g0a2ea0a-py3.4-linux-x86_64.egg
 /pandas/core/common.py in is_bool_dtype(arr_or_dtype)
 2459 
 2460 def is_bool_dtype(arr_or_dtype):
 -> 2461     tipo = _get_dtype_type(arr_or_dtype)
 2462     return issubclass(tipo, np.bool_)
 2463 

 /usr/local/lib/python3.4/dist-packages/pandas-0.15.1_91_g0a2ea0a-py3.4-linux-x86_64.egg
 /pandas/core/common.py in _get_dtype_type(arr_or_dtype)
 2388     elif isinstance(arr_or_dtype, CategoricalDtype):
 CategoricalDtypeType
 -> 2390     return arr_or_dtype.dtype.type
 2391 
 2392 

 AttributeError: 'NotImplementedType' object has no attribute 'dtype'

0 个答案:

没有答案