在熊猫中调用resample()时发生TypeError

时间:2018-10-29 14:13:05

标签: python python-3.x pandas pandas-datareader

好一阵子,直到我的整个代码块都让我头疼了好几天,我才接触代码。它一直有效直到遇到resample的以下挑战。

这是错误:

  

文件“ C:\ Users \ JonesDavid \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ pandas \ core \ generic.py”,行5522,重采样base=base, key=on, level=level)

     

文件“ C:\ Users \ JonesDavid \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ pandas \ core \ resample.py”,第999行,重采样return tg._get_resampler(obj, kind=kind)

     

_get_resampler "but got an instance of %r" % type(ax).__name__)中的文件“ C:\ Users \ JonesDavid \ Local \ Programs \ Python \ Python35 \ lib \ site-packages \ pandas \ core \ resample.py”第1116行

     

TypeError :仅对DatetimeIndex,TimedeltaIndex或PeriodIndex有效,但具有“ Index”的实例

  • 在doForecast中:tsWeekly = ts.resample('W-MON').last())
  • 主要:doForecast(panel)
  • 在:main()

这是我的整个代码减去与绘图相关的部分:

import pandas_datareader.data as web
from datetime import datetime
# removed imports related to plotting

TRAIN_SIZE = 0.2
FORECAST_STEPS = 20
STOCKS_PREDICT = ['SPY', 'AAPL']


def downloadData(startDate, endDate):

    histPanel = web.DataReader(STOCKS_PREDICT, 'iex' , startDate, endDate)
    contains_condition = ((histPanel.isnull()) | (histPanel == 0)).any(axis=1)
    to_keep = contains_condition[contains_condition == False].index
    histPanel = histPanel.loc[to_keep]

    return histPanel


print(downloadData("01/01/2017","01/01/2019"))


def doForecast(Panel):

    closeDF = Panel['close']
    ts = closeDF['SPY']
    tsWeekly = ts.resample('W-MON').last() # TypeError here
    values = tsWeekly.tolist()

    # removed unrelevant plotting code


def main():
    startDate = datetime(2017, 1, 1)
    endDate = datetime.today()

    panel = downloadData(startDate, endDate)

    doForecast(panel)


if __name__ == '__main__':
    main()

0 个答案:

没有答案