2016-09-19 107 views
3

我正在使用pandas创建一个python脚本来读取具有多个行值的文件。一旦读取,我需要构建这些值的数组,然后将其分配给数据帧行值。ProgrammingError:(psycopg2.ProgrammingError)无法调整类型'numpy.ndarray'

我用的代码是

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'

+1

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

回答

0

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

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

相关问题