我不能複製:
甲骨文:
create table tab1 (id number, col1 number);
create table tab2 (id number, col2 varchar2(10));
insert into tab1 values(1, 10);
insert into tab1 values(2, 11);
insert into tab1 values(3,12);
insert into tab2 values(1,'aaa');
insert into tab2 values(2,'bbb');
insert into tab2 values(3,'ccc');
commit;
的Python:
import pandas as pd
import cx_Oracle
from sqlalchemy import types, create_engine
usr = 'ora_user'
pwd = 'ora_pwd'
tns = """
(DESCRIPTION =
(ADDRESS = (PROTOCOL = TCP)(HOST = test-rac-scan.wirecard.sys)(PORT = 1521))
(CONNECT_DATA =
(SERVER = DEDICATED)
(SERVICE_NAME = <MY_SERVICE_NAME>.wirecard)
)
)
"""
qry = """
select a.col1,b.col2
from tab1 a, tab2 b
where a.id = b.id
"""
engine = create_engine('oracle+cx_oracle://%s:%[email protected]%s' % (usr, pwd, tns))
df = pd.read_sql(qry, engine)
結果:
In [12]: df
Out[12]:
col1 col2
0 10 aaa
1 11 bbb
2 12 ccc
In [13]: df.dtypes
Out[13]:
col1 int64 # <-------- NOTE !
col2 object
dtype: object
模塊的版本:
In [14]: cx_Oracle.__version__
Out[14]: '5.3'
In [15]: pd.__version__
Out[15]: '0.19.2'
In [17]: sqlalchemy.__version__
Out[17]: '1.1.5'
UPDATE:
每個數據幀列Pandas.Series類型的對象 - 它沒有任何與此列的D型:
In [50]: type(df['col1'])
Out[50]: pandas.core.series.Series
In [51]: type(df['col2'])
Out[51]: pandas.core.series.Series
In [52]: type(df)
Out[52]: pandas.core.frame.DataFrame
如果你想檢查dtype
(類似於Oracle列數據類型)使用DataFrame.dtypes
屬性:
In [53]: df.dtypes
Out[53]:
col1 float64
col2 object
dtype: object
你問題不清楚。哪些數據類型在Oracle中具有'a.col1'和'b.col2'? – MaxU
col1是一個數字,col2是varchar – abhi1489
那麼你會在Pandas中看到相應的'dtype'? – MaxU