ValueError:小时必须在0..23 pandas.to_sql()

时间:2018-09-16 16:05:51

标签: python pandas

我已经在另一台Mac上运行了这段代码,当我移到一个新的Mac上时,我开始看到此问题,它们具有相同的时区,并且我使用的是相同的Python版本,因此我做了一点工作找不到修复它的有用技巧,因为我可以看到数据看起来正常,但是由于某种原因pandas.to_sql()抱怨它:

更新

  • 使用Ubuntu Xenial进行测试-Python3.5-工作
  • Mac High Sierra 10.13.6-Python3.7-无法正常运行
  • Mac High Sierra 10.13.5-Python3.7-运行中

libs正在由requirements.txt文件处理。

我当前数据的示例:

0      2017-09-13 10:05:00.009
1      2017-09-13 10:05:00.009
2      2017-09-13 10:05:00.009
3      2017-09-13 10:05:00.009
4      2017-09-13 10:05:00.009
5      2017-09-13 10:05:00.009
6      2017-09-13 10:05:00.009

这是我的示例代码:

def RetrieveData(filename=None, papel=None):

    df = pd.read_csv("%s" %(filename), header=None, delimiter="\;",
                skiprows=1, engine='python',
                names=["Session Date", "symbol", "Deal Number", "Deal Price",
                "volume", "Hour", "Ind Cancel", "Offer Date", "Seq Offer Date",
                "GenerationID", "Deal Condition", "Date Sell Offer", "Sequence Sell Offer",
                "Generation Id Sell", "Sell Condition", "Indicator", "Broker Buy", "Broker Sell"],
                )

    df = df[df.symbol.str.contains(papel, na=False)]
    df['period'] = pd.to_datetime(df['Session Date']) + pd.to_timedelta(df['Hour'])
    print(df['period'])
    df.set_index('period', inplace=True)

    vol = df['volume'].resample('1d').sum()
    symbol = df['symbol'].resample('1d').sum()
    price = df['Deal Price'].resample('1d').ohlc()
    bars = pd.concat([price, vol, symbol], axis=1)

    bars.rename(columns={
                'open'  : 'ohlc_open',
                'close' : 'ohlc_close',
                'high'  : 'ohlc_high',
                'low'   : 'ohlc_low'}, inplace=True)

    engine = create_engine('sqlite:///database.db')
    bars.to_sql(name='symbol', con=engine, index=True, if_exists='append')

我的数据库定义:

class Symbol(Base):
    __tablename__ = 'Symbol'
    id = Column(Integer, primary_key=True)
    symbol = Column(String(250), nullable=False)
    volume = Column(Integer, nullable=False)
    ohlc_open = Column(Float(10), nullable=False)
    ohlc_high = Column(Float(10), nullable=False)
    ohlc_low  = Column(Float(10), nullable=False)
    ohlc_close = Column(Float(10), nullable=False)
    period = Column(DateTime)

例外情况:

 File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/core/generic.py", line 2130, in to_sql
    dtype=dtype)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/io/sql.py", line 450, in to_sql
    chunksize=chunksize, dtype=dtype)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/io/sql.py", line 1127, in to_sql
    table.insert(chunksize)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/io/sql.py", line 619, in insert
    keys, data_list = self.insert_data()
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pandas/io/sql.py", line 600, in insert_data
    d = b.values.astype('M8[us]').astype(object)
ValueError: hour must be in 0..23

0 个答案:

没有答案