6.3.5 Pythonデータセットからの永続データベース表の作成
oml.create
関数を使用して、Pythonセッションのデータからデータベース・スキーマ内に永続表を作成します。
oml.create
関数は、データベース・スキーマ内に表を作成し、表のプロキシであるoml.DataFrame
オブジェクトを返します。プロキシoml.DataFrame
オブジェクトの名前は表と同じです。
ノート:
Oracle Machine Learning for Pythonで表を作成するときに表の名前に小文字または大/小文字の組合せを使用する場合、SQL問合せまたはファンクションで表を使用するときは、同じ小文字または大/小文字の組合せの名前を二重引用符で囲って使用する必要があります。表を作成するときにすべて大文字の名前を使用する場合、表の名前は大/小文字が区別されません。二重引用符を使用せずに表を使用する場合、大文字、小文字、または大文字/小文字の組合せを使用できます。これは、表の列に名前を付ける場合も同様です。oml.drop
関数を使用して、データベース・スキーマ内の永続表を削除できます。
注意:
永続データベース表を削除するには、oml.drop
関数を使用します。oml.DataFrame
プロキシ・オブジェクトとそれに関連付けられた一時表を削除するには、del
文を使用します。del
では、永続表は削除されません。
oml.create
関数の構文は次のとおりです。
oml.create(x, table, oranumber=True, dbtypes=None, append=False)
x
引数は、pandas.DataFrame
または表のデータを含む同一サイズのタプルのリストです。タプルのリストでは、それぞれのタプルは表内の行を表し、列名はCOL1、COL2などに設定されます。table
引数は、表の名前を指定する文字列です。
列のSQLデータ型は、次によって決定されます。
-
OML4Pyでは、表からサンプリングされた20個のランダムな行を調べて、デフォルトの列型が決定されます。20行未満の表については、すべての行を使用して列型が決定されます。
列の値がすべて
None
である場合、または列について、サンプリングされた行にNone
ではない一貫性のないデータ型が含まれている場合は、dbtypes
引数で列のSQL型が指定されていないかぎり、デフォルトの列型を決定することはできず、ValueError
が発生します。 -
数値列については、
bool
であるoranumber
引数によってSQLデータ型が決定されます。True
(デフォルト)の場合、SQLデータ型はNUMBERです。False
の場合、データ型はBINARY DOUBLEです。x
のデータにNaN値が含まれている場合は、oranumber
をFalse
に設定する必要があります。 -
文字列列については、デフォルトの型はVARCHAR2(4000)です。
-
バイト列については、デフォルトの型はBLOBです。
dbtypes
パラメータを使用すると、表の列のSQLデータ型を指定できます。dbtypes
の値には、str
をstr
値にマップするdict
またはstr
値のリストを指定できます。dict
については、キーは列の名前です。append
引数がTrue
の場合、dbtypes
パラメータは無視されます。
append
引数は、x
のデータを既存の表に追加するかどうかを指定するbool
です。
例6-13 Pythonデータセットからのデータベース表の作成
この例では、データベース接続のcursor
オブジェクトを作成し、各種のデータ型の列を含むpandas.core.frame.DataFrame
を作成した後、様々なoml.create
パラメータを使用して一連の表を作成し、表の列のSQLデータ型を表示します。
import oml
# Create a cursor object for the current OML4Py database
# connection to run queries and get information from the database.
cr = oml.cursor()
import pandas as pd
df = pd.DataFrame({'numeric': [1, 1.4, -4, 3.145, 5, 2],
'string' : [None, None, 'a', 'a', 'a', 'b'],
'bytes' : [b'a', b'b', b'c', b'c', b'd', b'e']})
# Get the order of the columns
df.columns
# Create a table with the default parameters.
oml_df1 = oml.create(df, table = 'tbl1')
# Show the default SQL data types of the columns.
_ = cr.execute("select data_type from all_tab_columns where table_name = 'tbl1'")
cr.fetchall()
# Create a table with oranumber set to False.
oml_df2 = oml.create(df, table = 'tbl2', oranumber = False)
# Show the SQL data typea of the columns.
_ = cr.execute("select data_type from all_tab_columns where table_name = 'tbl2'")
cr.fetchall()
# Create a table with dbtypes specified as a dict mapping column names
# to SQL data types.
oml_df3 = oml.create(df, table = 'tbl3',
dbtypes = {'numeric': 'BINARY_DOUBLE',
'bytes':'RAW(1)'})
# Show the SQL data types of the columns.
_ = cr.execute("select data_type from all_tab_columns where table_name = 'tbl3'")
cr.fetchall()
# Create a table with dbtypes specified as a list of SQL data types
# matching the order of the columns.
oml_df4 = oml.create(df, table = 'tbl4',
dbtypes = ['BINARY_DOUBLE','VARCHAR2','RAW(1)'])
# Show the SQL data type of the columns.
_ = cr.execute("select data_type from all_tab_columns where table_name = 'tbl4'")
cr.fetchall()
# Create a table from a list of tuples.
lst = [(1, None, b'a'), (1.4, None, b'b'), (-4, 'a', b'c'),
(3.145, 'a', b'c'), (5, 'a', b'd'), (None, 'b', b'e')]
oml_df5 = oml.create(lst, table = 'tbl5',
dbtypes = ['BINARY_DOUBLE','CHAR(1)','RAW(1)'])
# Close the cursor
cr.close()
# Drop the tables.
oml.drop('tbl1')
oml.drop('tbl2')
oml.drop('tbl3')
oml.drop('tbl4')
oml.drop('tbl5')
この例のリスト
>>> import oml
>>>
>>> # Create a cursor object for the current OML4Py database
... # connection to run queries and get information from the database.
... cr = oml.cursor()
>>>
>>> import pandas as pd
>>>
>>> df = pd.DataFrame({'numeric': [1, 1.4, -4, 3.145, 5, 2],
... 'string' : [None, None, 'a', 'a', 'a', 'b'],
... 'bytes' : [b'a', b'b', b'c', b'c', b'd', b'e']})
>>>
>>> # Get the order of the columns.
... df.columns
Index(['numeric', 'string', 'bytes'], dtype='object')
>>>
>>> # Create a table with the default parameters.
... oml_df1 = oml.create(df, table = 'tbl1')
>>>
>>> # Show the default SQL data types of the columns.
... _ = cr.execute("select data_type from all_tab_columns where table_name = 'tbl1'")
>>> cr.fetchall()
[('NUMBER',), ('VARCHAR2',), ('BLOB',)]
>>>
>>> # Create a table with oranumber set to False.
... oml_df2 = oml.create(df, table = 'tbl2', oranumber = False)
>>>
>>> # Show the SQL data types of the columns.
... _ = cr.execute("select data_type from all_tab_columns where table_name = 'tbl2'")
>>> cr.fetchall()
[('BINARY_DOUBLE',), ('VARCHAR2',), ('BLOB',)]
>>>
>>> # Create a table with dbtypes specified as a dict mapping column names
... # to SQL data types.
... oml_df3 = oml.create(df, table = 'tbl3',
... dbtypes = {'numeric': 'BINARY_DOUBLE',
... 'bytes':'RAW(1)'})
>>>
>>> # Show the SQL data type of the columns.
... _ = cr.execute("select data_type from all_tab_columns where table_name = 'tbl3'")
>>> cr.fetchall()
[('BINARY_DOUBLE',), ('VARCHAR2',), ('RAW',)]
>>>
>>> # Create a table with dbtypes specified as a list of SQL data types
... # matching the order of the columns.
... oml_df4 = oml.create(df, table = 'tbl4',
... dbtypes = ['BINARY_DOUBLE','CHAR(1)', 'RAW(1)'])
>>>
>>> # Show the SQL data type of the columns
... _ = cr.execute("select data_type from all_tab_columns where table_name = 'tbl4'")
>>> cr.fetchall()
[('BINARY_DOUBLE',), ('CHAR',), ('RAW',)]
>>>
>>> # Create a table from a list of tuples.
... lst = [(1, None, b'a'), (1.4, None, b'b'), (-4, 'a', b'c'),
... (3.145, 'a', b'c'), (5, 'a', b'd'), (None, 'b', b'e')]
>>> oml_df5 = oml.create(lst, table ='tbl5',
... dbtypes = ['BINARY_DOUBLE','CHAR(1)','RAW(1)'])
>>>
>>> # Show the SQL data type of the columns.
... _ = cr.execute("select data_type from all_tab_columns where table_name = 'tbl5'")
>>> cr.fetchall()
[('BINARY_DOUBLE',), ('CHAR',), ('RAW',)]
>>>
>>> # Close the cursor.
... cr.close()
>>>
>>> # Drop the tables
... oml.drop('tbl1')
>>> oml.drop('tbl2')
>>> oml.drop('tbl3')
>>> oml.drop('tbl4')
>>> oml.drop('tbl5')
親トピック: データベースとPythonセッションの間のデータの移動