-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstore_data.py
More file actions
38 lines (32 loc) · 1.32 KB
/
store_data.py
File metadata and controls
38 lines (32 loc) · 1.32 KB
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
import pandas as pd
from sqlalchemy import create_engine
import mysql.connector
class DataStore(object):
"""
Connects to the database and stores data
"""
def __init__(self, database_ip, logger=None):
self.engine = None
self.ip = database_ip # 35.227.50.121
def connect_to_database(self):
"""
Connect to the database
"""
eng_str = 'mysql+mysqldb://{0}:{1}@{2}/{3}'.format('****',
'****',
self.ip,
'esschema')
self.engine = create_engine(eng_str, pool_recycle=3600, echo=False)
def chunker(self, seq, size):
# from http://stackoverflow.com/a/434328
return (seq[pos:pos + size] for pos in range(0, len(seq), size))
def store_data(self, dataframe):
"""
Store dataframe in a table
:param dataframe: Name of the dataframe and the MySQL table
"""
chunksize = int(len(dataframe) / 1000)
for i, cdf in enumerate(self.chunker(dataframe, chunksize)):
replace = "replace" if i == 0 else "append"
dataframe.to_sql(con=self.engine, name='energystats',
if_exists=replace, index=False)