指定备用日期时间格式"Exception: Invalid datetime. It must be bigger than that last one"



我正在尝试读取每 15 分钟有一个新的数据条目的 csv。 据我所知,我返回此异常的原因是因为日期不会每行都更改,但时间会更改。但是,提要没有及时读取,我不确定如何解决这个问题。这是我的代码:

from pyalgotrade.feed import csvfeed
feed = csvfeed.Feed("Date","%d/%m/%Y")
feed.addValuesFromCSV("eurusd-15m-july-small.csv")
for dateTime, value in feed:
print (dateTime, value)

我的csv看起来像这样:

Date,Time,Open,High,Low,Close,Volume
07/08/2018,17:30:00,1.15994,1.15994,1.15961,1.15982,414
07/08/2018,17:45:00,1.15982,1.16001,1.15964,1.15996,485

这是完整的错误:

(datetime.datetime(2018, 8, 7, 0, 0), {'High': 1.15994, 'Volume': 414.0, 
'Low': 1.15961, 'Time': '17:30:00', 'Close': 1.15982, 'Open': 1.15994})
---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)
<ipython-input-12-0cbdbe588a05> in <module>()
3 feed = csvfeed.Feed("Date","%d/%m/%Y")
4 feed.addValuesFromCSV("eurusd-15m-july-small.csv")
----> 5 for dateTime, value in feed:
6     print (dateTime, value)
/Users/Phil/anaconda2/lib/python2.7/site- 
packages/pyalgotrade/feed/__init__.pyc in feed_iterator(feed)
29     try:
30         while not feed.eof():
---> 31             yield feed.getNextValuesAndUpdateDS()
32     finally:
33         feed.stop()
/Users/Phil/anaconda2/lib/python2.7/site- 
packages/pyalgotrade/feed/__init__.pyc in getNextValuesAndUpdateDS(self)
88                     ds = self.createDataSeries(key, self.__maxLen)
89                     self.__ds[key] = ds
---> 90                 ds.appendWithDateTime(dateTime, value)
91         return (dateTime, values)
92 
/Users/Phil/anaconda2/lib/python2.7/site- 
packages/pyalgotrade/dataseries/__init__.pyc in appendWithDateTime(self, 
dateTime, value)
134 
135         if dateTime is not None and len(self.__dateTimes) != 0 and 
self.__dateTimes[-1] >= dateTime:
--> 136             raise Exception("Invalid datetime. It must be bigger than 
that last one")
137 
138         assert(len(self.__values) == len(self.__dateTimes))
Exception: Invalid datetime. It must be bigger than that last one

提前感谢!

问题是您将日期时间放在两个单独的列中,因此@daniel提到您必须自定义解析。试试这个:

import datetime
from pyalgotrade.feed import csvfeed

# Row parser that extracts the datetime combining 2 columns
class RowParser(csvfeed.RowParser):
def parseRow(self, csvRowDict):
dateTimeCombined = "%s %s" % (csvRowDict["Date"], csvRowDict["Time"])
dateTime = datetime.datetime.strptime(dateTimeCombined, "%d/%m/%Y %H:%M:%S")
# Convert the values
values = {}
for key, value in csvRowDict.items():
if key not in ["Date", "Time"]:
values[key] = csvfeed.float_or_string(key, value)
return (dateTime, values)
def getFieldNames(self):
return None
def getDelimiter(self):
return ","

feed = csvfeed.BaseFeed(RowParser())
feed.addValuesFromCSV("eurusd-15m-july-small.csv")
for dateTime, value in feed:
print (dateTime, value)

相关内容

  • 没有找到相关文章

最新更新