cx_Oracle相当于datetime64 [ns]

时间:2016-04-26 15:28:10

标签: oracle python-3.x pandas cx-oracle

在Pandas数据帧的列中给出以下类型的数据(Pandas数据类型是datetime64 [ns]:

Contract_EffDt
1974-02-01

我想将此(以及一些字符串和整数列)导入Oracle,如下所示:

import cx_Oracle as cx
.
.
.
cur.setinputsizes(25,25,255,255,25,25,25,255,255,25,25,cx.DATE,int)
cur=con.cursor()
cur.bindarraysize = 1000
cur.executemany('''insert into Table(Contract_ID,Plan_ID,Org_Type,Plan_Type,Offers_Part_D,SNP_Plan,\
EGHP,Org_Name,Org_Mkt_Name,Plan_Name,Parent_Org,Contract_EffDt,YYYYMM) values (:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,:12,:13)''', rows)
con.commit()
cur.close()
con.close()

如何让cx_Oracle识别我的datetime列是什么,并相应地插入它?正如您所看到的,我已经尝试过cx.DATE,但这不起作用。

提前致谢!

更新更多信息:

import cx_Oracle as cx
import pandas as pd
pwd=pwd
namec='table'
con = cx.Connection("table/"+pwd+"@server")
dfc=pd.read_csv(r'\\...\CPSC_Contract_Info_2016_03.csv',
                encoding='latin-1')
#taken from https://www.cms.gov/Research-Statistics-Data-and-Systems/Statistics-Trends-and-Reports/MCRAdvPartDEnrolData/Downloads/2016/March/CPSC-Enrollment-2016-03.zip 
#^contract info file^
rows=dfc.values.tolist()
dfc['Contract_EffDt'] = pd.to_datetime(dfc['Contract_EffDt'])
dfc['Contract_EffDt'] = pd.to_datetime(dfc['Contract_EffDt'],format='%YYYY-%mm-%dd')
dfc['Plan_ID']=dfc['Plan_ID'].astype(str)
dfc.dtypes

Contract_ID               object
Plan_ID                   object
Org_Type                  object
Plan_Type                 object
Offers_Part_D             object
SNP_Plan                  object
EGHP                      object
Org_Name                  object
Org_Mkt_Name              object
Plan_Name                 object
Parent_Org                object
Contract_EffDt    datetime64[ns]
YYYYMM                     int64
dtype: object

cur.setinputsizes(25,25,255,255,25,25,25,255,255,25,25,cx.DATETIME,int)
cur=con.cursor()
cur.bindarraysize = 1000
cur.executemany('''insert into table(Contract_ID,Plan_ID,Org_Type,Plan_Type,Offers_Part_D,SNP_Plan,\
EGHP,Org_Name,Org_Mkt_Name,Plan_Name,Parent_Org,Contract_EffDt,YYYYMM) values (:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,to_date(:12,'yyyy-mm-dd'),:13)''', rows)
con.commit()
cur.close()
con.close()

输出:

TypeError                                 Traceback (most recent call last)
<ipython-input-152-6696ee05e0f2> in <module>()
      4 cur=con.cursor()
      5 cur.bindarraysize = 1000
----> 6 cur.executemany('''insert into table(Contract_ID,Plan_ID,Org_Type,Plan_Type,Offers_Part_D,SNP_Plan,EGHP,Org_Name,Org_Mkt_Name,Plan_Name,Parent_Org,Contract_EffDt,YYYYMM) values (:1,:2,:3,:4,:5,:6,:7,:8,:9,:10,:11,to_date(:12,'yyyy-mm-dd'),:13)''', rows)
      7 con.commit()
      8 cur.close()


TypeError: expecting numeric data

0 个答案:

没有答案