forked from pandas-dev/pandas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathio_sql.py
126 lines (95 loc) · 4.36 KB
/
io_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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
from vbench.api import Benchmark
from datetime import datetime
common_setup = """from pandas_vb_common import *
import sqlite3
import sqlalchemy
from sqlalchemy import create_engine
engine = create_engine('sqlite:///:memory:')
con = sqlite3.connect(':memory:')
"""
sdate = datetime(2014, 6, 1)
#-------------------------------------------------------------------------------
# to_sql
setup = common_setup + """
index = tm.makeStringIndex(10000)
df = DataFrame({'float1' : randn(10000),
'float2' : randn(10000),
'string1' : ['foo'] * 10000,
'bool1' : [True] * 10000,
'int1' : np.random.randint(0, 100000, size=10000)},
index=index)
"""
sql_write_sqlalchemy = Benchmark("df.to_sql('test1', engine, if_exists='replace')",
setup, start_date=sdate)
sql_write_fallback = Benchmark("df.to_sql('test1', con, if_exists='replace')",
setup, start_date=sdate)
#-------------------------------------------------------------------------------
# read_sql
setup = common_setup + """
index = tm.makeStringIndex(10000)
df = DataFrame({'float1' : randn(10000),
'float2' : randn(10000),
'string1' : ['foo'] * 10000,
'bool1' : [True] * 10000,
'int1' : np.random.randint(0, 100000, size=10000)},
index=index)
df.to_sql('test2', engine, if_exists='replace')
df.to_sql('test2', con, if_exists='replace')
"""
sql_read_query_sqlalchemy = Benchmark("read_sql_query('SELECT * FROM test2', engine)",
setup, start_date=sdate)
sql_read_query_fallback = Benchmark("read_sql_query('SELECT * FROM test2', con)",
setup, start_date=sdate)
sql_read_table_sqlalchemy = Benchmark("read_sql_table('test2', engine)",
setup, start_date=sdate)
#-------------------------------------------------------------------------------
# type specific write
setup = common_setup + """
df = DataFrame({'float' : randn(10000),
'string' : ['foo'] * 10000,
'bool' : [True] * 10000,
'datetime' : date_range('2000-01-01', periods=10000, freq='s')})
df.loc[1000:3000, 'float'] = np.nan
"""
sql_float_write_sqlalchemy = \
Benchmark("df[['float']].to_sql('test_float', engine, if_exists='replace')",
setup, start_date=sdate)
sql_float_write_fallback = \
Benchmark("df[['float']].to_sql('test_float', con, if_exists='replace')",
setup, start_date=sdate)
sql_string_write_sqlalchemy = \
Benchmark("df[['string']].to_sql('test_string', engine, if_exists='replace')",
setup, start_date=sdate)
sql_string_write_fallback = \
Benchmark("df[['string']].to_sql('test_string', con, if_exists='replace')",
setup, start_date=sdate)
sql_datetime_write_sqlalchemy = \
Benchmark("df[['datetime']].to_sql('test_datetime', engine, if_exists='replace')",
setup, start_date=sdate)
#sql_datetime_write_fallback = \
# Benchmark("df[['datetime']].to_sql('test_datetime', con, if_exists='replace')",
# setup3, start_date=sdate)
#-------------------------------------------------------------------------------
# type specific read
setup = common_setup + """
df = DataFrame({'float' : randn(10000),
'datetime' : date_range('2000-01-01', periods=10000, freq='s')})
df['datetime_string'] = df['datetime'].map(str)
df.to_sql('test_type', engine, if_exists='replace')
df[['float', 'datetime_string']].to_sql('test_type', con, if_exists='replace')
"""
sql_float_read_query_sqlalchemy = \
Benchmark("read_sql_query('SELECT float FROM test_type', engine)",
setup, start_date=sdate)
sql_float_read_table_sqlalchemy = \
Benchmark("read_sql_table('test_type', engine, columns=['float'])",
setup, start_date=sdate)
sql_float_read_query_fallback = \
Benchmark("read_sql_query('SELECT float FROM test_type', con)",
setup, start_date=sdate)
sql_datetime_read_as_native_sqlalchemy = \
Benchmark("read_sql_table('test_type', engine, columns=['datetime'])",
setup, start_date=sdate)
sql_datetime_read_and_parse_sqlalchemy = \
Benchmark("read_sql_table('test_type', engine, columns=['datetime_string'], parse_dates=['datetime_string'])",
setup, start_date=sdate)