【问题标题】:ProgrammingError: (psycopg2.ProgrammingError) can't adapt type 'numpy.ndarray'ProgrammingError:(psycopg2.ProgrammingError)无法适应类型'numpy.ndarray'
【发布时间】:2021-03-30 01:33:32
【问题描述】:

我正在使用 pandas 创建一个 python 脚本来读取具有多个行值的文件。

读取后,我需要构建这些值的数组,然后将其分配给数据框行值。

我使用的代码是

import re
import numpy as np
import pandas as pd
master_data = pd.DataFrame()
temp_df = pd.DataFrame()
new_df = pd.DataFrame()

for f in data:


##Reading the file in pandas which is in excel format 
#
file_df = pd.read_excel(f)


filename = file_df['Unnamed: 1'][2]


##Skipping first 24 rows to get the required reading values
column_names = ['start_time','xxx_value']
data_df = pd.read_excel(f, names=column_names, skiprows=25)


array =np.array([])

   for i in data_df.iterrows():
       array = np.append(array,i[1][1])


   temp_df['xxx_value'] = [array]
   temp_df['Filename'] = filename
   temp_df['sub_id']=       
   temp_df['Filename'].str.split('_',1).str[1].str.strip() 
   temp_df['sen_site']=    
   temp_df['Filename'].str.split('_',1).str[0].str.strip()
   temp_df['sampling_interval'] = 15
   temp_df['start_time'] = data_df['start_time'][2]


   new_df= new_df.append(xxx_df)

   new_df.index = new_df.index + 1
   new_df=new_df.sort_index()
   new_df.index.name='record_id'

  new_df = new_df.drop("Filename",1)    ##dropping the Filename as it          
  is not needed to be loaded in postgresql

 ##Rearrange to postgresql format
 column_new_df = new_df.columns.tolist()
 column_new_df.
 insert(4,column_new_df.pop(column_new_df.index('xxx_value')))
 new_df = new_df.reindex(columns = column_new_df)

 print(new_df)

当我尝试将数组数据插入 Postgresql 时,此代码不起作用。

它给了我一个错误说明:

ProgrammingError: (psycopg2.ProgrammingError) 无法适配类型 'numpy.ndarray'

【问题讨论】:

  • 您好,我遇到了同样的问题,在寻找解决方案时遇到了您的问题。因此,我认为解决它对未来的其他人来说是有价值的。您能否修复您提供的代码示例(for f in data: 之后的缩进错误)?另外,哪一行抛出错误?

标签: python numpy


【解决方案1】:

我不确定问题出在哪里,因为我在您的代码中看不到您将数据插入 Postgres 的部分。

我的猜测是你给 Postgres 一个 Numpy 数组:psycopg2 不能处理 Numpy 数据类型,但它应该很容易将它转换为与 psycopg2 一起工作的本机 Python 类型(例如,通过使用 .tolist(方法),没有代码很难给出更精确的信息)。

【讨论】:

    【解决方案2】:

    在我看来,最有效的方法是让 psycopg2 始终了解 np.ndarray(s)。可以通过注册适配器来做到这一点:

    import numpy as np
    from psycopg2.extensions import register_adapter, AsIs
    
    def addapt_numpy_array(numpy_array):
        return AsIs(tuple(numpy_array))
    
    register_adapter(np.ndarray, addapt_numpy_array)
    

    为了帮助总体上使用 numpy,我对依赖于 psycopg2 的脚本/库的默认插件是:

    import numpy as np
    from psycopg2.extensions import register_adapter, AsIs
    
    def addapt_numpy_float64(numpy_float64):
        return AsIs(numpy_float64)
    
    def addapt_numpy_int64(numpy_int64):
        return AsIs(numpy_int64)
    
    def addapt_numpy_float32(numpy_float32):
        return AsIs(numpy_float32)
    
    def addapt_numpy_int32(numpy_int32):
        return AsIs(numpy_int32)
    
    def addapt_numpy_array(numpy_array):
        return AsIs(tuple(numpy_array))
    
    register_adapter(np.float64, addapt_numpy_float64)
    register_adapter(np.int64, addapt_numpy_int64)
    register_adapter(np.float32, addapt_numpy_float32)
    register_adapter(np.int32, addapt_numpy_int32)
    register_adapter(np.ndarray, addapt_numpy_array)
    

    否则即使是数字类型也会有一些问题。

    我从另一个 stackoverflow entry 那里得到了适配器技巧。

    【讨论】:

    • 我正在尝试您的解决方案,但出现错误...SyntaxError: syntax error at or near "[" 有什么解决办法吗?
    • 这个错误似乎与错误发生之前的一些错误的右括号或括号有关。据我所知,这里的代码块正确地关闭了所有打开的括号,所以错误应该来自代码中的其他地方?我需要查看更多上下文才能真正查明问题出在哪里......
    • 我必须这样做:``` def addapt_numpy_array(numpy_array): if len(numpy_array) == 0: return AsIs("null") return AsIs("ARRAY" + np. array2string(numpy_array, separator=",")) ```
    【解决方案3】:

    首先使用 applytolist 将每个 numpy 数组元素转换为其等效列表,然后您应该能够将数据写入 Postgres:

    df['column_name'] = df['column_name'].apply(lambda x: x.tolist())
    

    【讨论】:

      【解决方案4】:

      我们可以通过一次提取一个元素来解决这个问题。这里我假设一个temp_dfsub_id 类型为numpy.int64 的数据框,我们可以使用iloctemp_df.iloc[0]['sub_id'].item() 直接提取值,我们可以将其推送到数据库中。

      【讨论】:

        猜你喜欢
        • 2021-08-08
        • 2019-11-10
        • 2020-08-21
        • 1970-01-01
        • 1970-01-01
        • 2016-10-19
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多