尝试使用to_sql将pandas dataframe写入oracle时出错

时间:2014-06-04 08:27:23

标签: python pandas

Python:2.7.6 熊猫:0.14 SQLAlchemy:0.9.4

尝试使用Pandas to_sql函数将数据帧写入Oracle DB时,出现以下错误

TypeError: expecting string, unicode or buffer object

Dataframe是通过将2个单独的数据帧附加在一起创建的,df1和df2。 df1信息如下:

Int64Index: 20 entries, 0 to 19
Data columns (total 19 columns):
AID                20 non-null int64
CHARGE_OFF_AMT     20 non-null object
FEE_PAID           20 non-null object
GRADE              20 non-null object
INT_PMT            20 non-null object
LATE_FEE_PMT       20 non-null object
OUTSTANDING_AMT    20 non-null object
PERIOD             20 non-null object
PRIN_PMT           20 non-null object
PRODUCT            20 non-null int64
PURCHASES_AMT      20 non-null object
PURCHASES_COUNT    20 non-null int64
RECOVERY_PMT       20 non-null object
TERM               20 non-null int64
TOTAL_PMT          20 non-null object
WHOLE_FLAG         20 non-null object
ORDERS_AMT         16 non-null float64
ORDERS_COUNT       16 non-null float64
IN_FUNDING         16 non-null float64
dtypes: float64(3), int64(4), object(12)

df2信息如下:

Int64Index: 1 entries, 0 to 0
Data columns (total 6 columns):
AID            1 non-null int64
PERIOD         1 non-null object
DEPOSITS       1 non-null int64
WITHDRAWALS    1 non-null int64
BONUS          1 non-null int64
TOTAL_CASH     1 non-null object
dtypes: int64(4), object(2)

然后我将df2附加到df1,结果是final_d:

>>final_d = df1.append(df2)
>>final_d.info()
Int64Index: 21 entries, 0 to 0
Data columns (total 23 columns):
AID                21 non-null int64
BONUS              1 non-null float64
CHARGE_OFF_AMT     20 non-null object
DEPOSITS           1 non-null float64
FEE_PAID           20 non-null object
GRADE              20 non-null object
INT_PMT            20 non-null object
IN_FUNDING         16 non-null float64
LATE_FEE_PMT       20 non-null object
ORDERS_AMT         16 non-null float64
ORDERS_COUNT       16 non-null float64
OUTSTANDING_AMT    20 non-null object
PERIOD             21 non-null object
PRIN_PMT           20 non-null object
PRODUCT            20 non-null float64
PURCHASES_AMT      20 non-null object
PURCHASES_COUNT    20 non-null float64
RECOVERY_PMT       20 non-null object
TERM               20 non-null float64
TOTAL_CASH         1 non-null object
TOTAL_PMT          20 non-null object
WHOLE_FLAG         20 non-null object
WITHDRAWALS        1 non-null float64
dtypes: float64(9), int64(1), object(13)

最后,我试图将final_d写入oracle 11g数据库。

>>db = sqlalchemy.create_engine("oracle+cx_oracle://foo:bar@(DESCRIPTION = (LOAD_BALANCE=on) (FAILOVER=ON) \
              (ADDRESS = (PROTOCOL = TCP)(HOST = host.address.com)(PORT = 1521)) (CONNECT_DATA = (SERVER = DEDICATED) \
               (SERVICE_NAME = replica)))")
    notes_cash = all_notes_info.append(all_cash_info)
>>final_d.to_sql('data_table', db, if_exists='append', index=True)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/adsouza/anaconda/lib/python2.7/site-packages/pandas/core/generic.py", line 950, in to_sql
    index_label=index_label)
  File "/Users/adsouza/anaconda/lib/python2.7/site-packages/pandas/io/sql.py", line 440, in to_sql
    index_label=index_label)
  File "/Users/adsouza/anaconda/lib/python2.7/site-packages/pandas/io/sql.py", line 815, in to_sql
    table.insert()
  File "/Users/adsouza/anaconda/lib/python2.7/site-packages/pandas/io/sql.py", line 584, in insert
    self.pd_sql.execute(ins, data_list)
  File "/Users/adsouza/anaconda/lib/python2.7/site-packages/pandas/io/sql.py", line 783, in execute
    return self.engine.execute(*args, **kwargs)
  File "/Users/adsouza/anaconda/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1682, in execute
    return connection.execute(statement, *multiparams, **params)
  File "/Users/adsouza/anaconda/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 720, in execute
    return meth(self, multiparams, params)
  File "/Users/adsouza/anaconda/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 317, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "/Users/adsouza/anaconda/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 817, in _execute_clauseelement
    compiled_sql, distilled_params
  File "/Users/adsouza/anaconda/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 947, in _execute_context
    context)
  File "/Users/adsouza/anaconda/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1111, in _handle_dbapi_exception
    util.reraise(*exc_info)
  File "/Users/adsouza/anaconda/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 917, in _execute_context
    context)
  File "/Users/adsouza/anaconda/lib/python2.7/site-packages/sqlalchemy/dialects/oracle/cx_oracle.py", line 916, in do_executemany
    cursor.executemany(statement, parameters)
TypeError: expecting string, unicode or buffer object

我能够使用这种方法成功地将df1和df2写入oracle但是无法编写附加的数据帧。

以下是最终df的示例:

      AID       BONUS      CHARGE_OFF_AMT       DEPOSITS  FEE_PAID   GRADE  \
0  5819681       NaN      36600.565886430978       NaN        0        E   

        INT_PMT          IN_FUNDING  LATE_FEE_PMT  ORDERS_AMT     ...      \
0  113099.457782700363      257875        76.07     1091450     ...       

       PRIN_PMT          PRODUCT    PURCHASES_AMT PURCHASES_COUNT  RECOVERY_PMT  \
0  290035.650142431736       1        574400            2767             0   

  TERM  TOTAL_CASH            TOTAL_PMT  WHOLE_FLAG WITHDRAWALS  
0   36         NaN  403211.177925132099  FRACTIONAL         NaN

0 个答案:

没有答案