本文整理匯總了Python中pandas.io.sql.SQLTable方法的典型用法代碼示例。如果您正苦於以下問題:Python sql.SQLTable方法的具體用法?Python sql.SQLTable怎麽用?Python sql.SQLTable使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類pandas.io.sql
的用法示例。
在下文中一共展示了sql.SQLTable方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: test_sqlalchemy_type_mapping
# 需要導入模塊: from pandas.io import sql [as 別名]
# 或者: from pandas.io.sql import SQLTable [as 別名]
def test_sqlalchemy_type_mapping(self):
# Test Timestamp objects (no datetime64 because of timezone) (GH9085)
df = DataFrame({'time': to_datetime(['201412120154', '201412110254'],
utc=True)})
db = sql.SQLDatabase(self.conn)
table = sql.SQLTable("test_type", db, frame=df)
# GH 9086: TIMESTAMP is the suggested type for datetimes with timezones
assert isinstance(table.table.c['time'].type, sqltypes.TIMESTAMP)
示例2: to_sql
# 需要導入模塊: from pandas.io import sql [as 別名]
# 或者: from pandas.io.sql import SQLTable [as 別名]
def to_sql(df, name, schema, con, index, if_exists, mode='default', **kwargs):
"""
Override the default `pandas.to_sql` method to allow for insertion of
multiple rows of data at once. This is derived from the upstream patch at
https://github.com/pandas-dev/pandas/pull/21401, and can be deprecated
once it is merged and released in a new version of `pandas`.
"""
assert mode in ('default', 'multi'), 'unexpected `to_sql` mode {}'.format(mode)
if mode == 'default':
return df.to_sql(
name=name, schema=schema, con=con, index=index, if_exists=if_exists, **kwargs
)
else:
nrows = len(df)
if nrows == 0:
return
chunksize = kwargs.get('chunksize', nrows)
if chunksize == 0:
raise ValueError('chunksize argument should be non-zero')
chunks = int(nrows / chunksize) + 1
pd_sql = SQLDatabase(con)
pd_table = SQLTable(
name, pd_sql, frame=df, index=index, if_exists=if_exists,
index_label=kwargs.get('insert_label'), schema=schema, dtype=kwargs.get('dtype')
)
pd_table.create()
keys, data_list = pd_table.insert_data()
with pd_sql.run_transaction() as conn:
for i in range(chunks):
start_i = i * chunksize
end_i = min((i + 1) * chunksize, nrows)
if start_i >= end_i:
break
chunk_iter = zip(*[arr[start_i:end_i] for arr in data_list])
data = [{k: v for k, v in zip(keys, row)} for row in chunk_iter]
conn.execute(pd_table.table.insert(data)) # multivalues insert
示例3: test_sqlalchemy_type_mapping
# 需要導入模塊: from pandas.io import sql [as 別名]
# 或者: from pandas.io.sql import SQLTable [as 別名]
def test_sqlalchemy_type_mapping(self):
# Test Timestamp objects (no datetime64 because of timezone) (GH9085)
df = DataFrame({'time': to_datetime(['201412120154', '201412110254'],
utc=True)})
db = sql.SQLDatabase(self.conn)
table = sql.SQLTable("test_type", db, frame=df)
assert isinstance(table.table.c['time'].type, sqltypes.DateTime)