2016-09-19 105 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 (方法),沒有代碼就難以提供更精確的信息)。

相關問題