7.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値が含まれている場合は、oranumberFalseに設定する必要があります。

  • 文字列列については、デフォルトの型はVARCHAR2(4000)です。

  • バイト列については、デフォルトの型はBLOBです。

dbtypesパラメータを使用すると、表の列のSQLデータ型を指定できます。dbtypesの値には、strstr値にマップするdictまたはstr値のリストを指定できます。dictについては、キーは列の名前です。append引数がTrueの場合、dbtypesパラメータは無視されます。

append引数は、xのデータを既存の表に追加するかどうかを指定するboolです。

例7-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')