>  Q&A  >  본문

python - 使用pandas的resample报错

错误如下:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\Users\Administrator\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 699, in runfile
    execfile(filename, namespace)
  File "D:\Users\Administrator\Anaconda2\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 74, in execfile
    exec(compile(scripttext, filename, 'exec'), glob, loc)
  File "C:/Users/Administrator/Documents/Python Scripts/untitled1.py", line 24, in <module>
    s3=s2.resample('5min', how=ohlc_dict, closed='left', label='left')
  File "D:\Users\Administrator\Anaconda2\lib\site-packages\pandas\core\generic.py", line 4212, in resample
    base=base, key=on, level=level)
  File "D:\Users\Administrator\Anaconda2\lib\site-packages\pandas\tseries\resample.py", line 944, in resample
    return tg._get_resampler(obj, kind=kind)
  File "D:\Users\Administrator\Anaconda2\lib\site-packages\pandas\tseries\resample.py", line 1057, in _get_resampler
    "but got an instance of %r" % type(ax).__name__)
TypeError: Only valid with DatetimeIndex, TimedeltaIndex or PeriodIndex, but got an instance of 'Index'

我的代码如下:

names = ['date',
         'time',
         'open',
         'high',
         'low',
         'close',
         'vol',
         'amount']
s2=pd.read_csv('E:/test/SZ399920.csv',names=names, header=None,  index_col='date')

ohlc_dict = {                                                                                                             
'open':'first',                                                                                                    
'high':'max',                                                                                                       
'low':'min',                                                                                                        
'close': 'last',                                                                                                    
'vol': 'sum'
}


s3=s2.resample('5min', how=ohlc_dict, closed='left', label='left')


dataframe文件的格式如下:

巴扎黑巴扎黑2712일 전1214

모든 응답(1)나는 대답할 것이다

  • 高洛峰

    高洛峰2017-04-18 10:34:37

    먼저 Index를 DatetimeIndex로 변경해야 합니다. 5분으로 리샘플링하려면 인덱스에 시간도 입력해야 합니다.

    으아아아

    회신하다
    0
  • 취소회신하다