forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsql.py
84 lines (66 loc) · 3.42 KB
/
sql.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import sqlite3
import numpy as np
import pandas.util.testing as tm
from pandas import DataFrame, date_range, read_sql_query, read_sql_table
from sqlalchemy import create_engine
from ..pandas_vb_common import setup # noqa
class SQL(object):
goal_time = 0.2
params = (['sqlalchemy', 'sqlite'],
['float', 'float_with_nan', 'string', 'bool', 'int', 'datetime'])
param_names = ['connection', 'dtype']
def setup(self, connection, dtype):
N = 10000
con = {'sqlalchemy': create_engine('sqlite:///:memory:'),
'sqlite': sqlite3.connect(':memory:')}
self.table_name = 'test_type'
self.query_all = 'SELECT * FROM {}'.format(self.table_name)
self.query_col = 'SELECT {} FROM {}'.format(dtype, self.table_name)
self.con = con[connection]
self.df = DataFrame({'float': np.random.randn(N),
'float_with_nan': np.random.randn(N),
'string': ['foo'] * N,
'bool': [True] * N,
'int': np.random.randint(0, N, size=N),
'datetime': date_range('2000-01-01',
periods=N,
freq='s')},
index=tm.makeStringIndex(N))
self.df.loc[1000:3000, 'float_with_nan'] = np.nan
self.df['datetime_string'] = self.df['datetime'].astype(str)
self.df.to_sql(self.table_name, self.con, if_exists='replace')
def time_to_sql_dataframe_full(self, connection, dtype):
self.df.to_sql('test1', self.con, if_exists='replace')
def time_to_sql_dataframe_colums(self, connection, dtype):
self.df[[dtype]].to_sql('test1', self.con, if_exists='replace')
def time_read_sql_query_select_all(self, connection, dtype):
read_sql_query(self.query_all, self.con)
def time_read_sql_query_select_column(self, connection, dtype):
read_sql_query(self.query_all, self.con)
class ReadSQLTable(object):
goal_time = 0.2
params = ['float', 'float_with_nan', 'string', 'bool', 'int', 'datetime']
param_names = ['dtype']
def setup(self, dtype):
N = 10000
self.table_name = 'test'
self.con = create_engine('sqlite:///:memory:')
self.df = DataFrame({'float': np.random.randn(N),
'float_with_nan': np.random.randn(N),
'string': ['foo'] * N,
'bool': [True] * N,
'int': np.random.randint(0, N, size=N),
'datetime': date_range('2000-01-01',
periods=N,
freq='s')},
index=tm.makeStringIndex(N))
self.df.loc[1000:3000, 'float_with_nan'] = np.nan
self.df['datetime_string'] = self.df['datetime'].astype(str)
self.df.to_sql(self.table_name, self.con, if_exists='replace')
def time_read_sql_table_all(self, dtype):
read_sql_table(self.table_name, self.con)
def time_read_sql_table_column(self, dtype):
read_sql_table(self.table_name, self.con, columns=[dtype])
def time_read_sql_table_parse_dates(self, dtype):
read_sql_table(self.table_name, self.con, columns=['datetime_string'],
parse_dates=['datetime_string'])