【问题标题】:to_sql error: Unknown object type numpy.ndarray during describeto_sql 错误:描述期间的未知对象类型 numpy.ndarray
【发布时间】:2018-03-21 17:26:52
【问题描述】:

我正在尝试将数据框插入到 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 item sols value

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

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

【问题讨论】:

  • 请发布merged.info()的输出。
  • Int64Index:710719 个条目,0 到 710718 数据列(共 5 列):monthenddate 710719 非空对象 lastweekday 710719 非空对象项 710719 非空对象 sedols 710719 非空对象值 710719非空 float64 dtypes:float64(1),object(4) 内存使用:32.5+ MB 无
  • 我在答案中添加了更多代码,您可以使用这些代码来检查列中的数据类型。

标签: python sql-insert pandas-to-sql


【解决方案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('-----')

【讨论】:

    猜你喜欢
    • 2019-05-27
    • 2021-05-31
    • 2021-11-16
    • 2014-02-11
    • 2021-06-26
    • 2022-12-20
    • 1970-01-01
    • 2021-02-23
    • 1970-01-01
    相关资源
    最近更新 更多