0

我正在尝试将数据框插入到 sql 表中,但出现以下错误

ProgrammingError: (pyodbc.ProgrammingError) ('Unknown object type numpy.ndarray during describe', 'HY000')

对于以下代码

merged.to_sql('pmg.cwc.EmSignals1', engine, chunksize=1000, 
           if_exists='replace', 
           index=False, 
            dtype ={'monthenddate': sqlalchemy.types.NVARCHAR(length=20),                                                                                                       
           'lastweekday': sqlalchemy.types.NVARCHAR(length=20),
           'item': sqlalchemy.types.NVARCHAR(length=20),
           'sols': sqlalchemy.types.NVARCHAR(length=20),
           'value': sqlalchemy.types.NVARCHAR(length=20)})

这是数据框的 .head

monthenddate lastweekday 项目 sols 值

0 1999-12-31 1999-12-31 值 2W063W1 -0.870225

1 1999-12-31 1999-12-31 值 W1YBRK4 0.078154

2 1999-12-31 1999-12-31 值 X16W902 -0.072731

3 1999-12-31 1999-12-31 值 2X45X4W 1.278582

4 1999-12-31 1999-12-31 值 23X1XWX 0.293649

我已经尝试了很多,但无法找出问题的原因。

4

1 回答 1

1

我假设出于某种原因,其中一列包含 numpy 数组。您可以使用print(merged.info()).

如果是这种情况,请检查将数据分配给 DataFrame 的语句。

编辑:如print(merged.info())object列所示,您仍然不知道其中哪些可能是 numpy 数组。试试这个代码来深入挖掘:

for el in merged.iloc[0, :]:
    print('Checking: {:s}.'.format(str(el)))
    print(isinstance(el, np.generic))
    try:
        print(el.shape)
        print('Is a NumPy array.')
    except:
        print('Is not a NumPy array.')
    finally:
        print('-----')
于 2018-03-22T10:44:56.023 回答