suchen

Heim  >  Fragen und Antworten  >  Hauptteil

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文件的格式如下:

巴扎黑巴扎黑2803 Tage vor1263

Antworte allen(1)Ich werde antworten

  • 高洛峰

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

    你应该先把Index变成DatetimeIndex。你想resample到5分钟的话你也要把time放Index里:

    df = pd.DataFrame({'date': ["2008/07/01","2008/07/01","2008/07/01","2008/07/01","2008/07/01","2008/07/01","2008/07/01","2008/07/01"],
                      'time': ['09:31', '09:32','09:33','09:34','09:35','09:36','09:37', '09:38'],
                      'vals': [1, 2, 3, 4, 5, 6, 7, 8]})
    df2 = df.set_index(pd.DatetimeIndex(pd.to_datetime(df.date + " " + df.time)))
    
    df2.resample("5min", how={'vals': 'mean'})

    Antwort
    0
  • StornierenAntwort