Created
February 24, 2020 10:55
-
-
Save jamescalam/9532e8813f8161e66f420b0f427e7c87 to your computer and use it in GitHub Desktop.
Alternative push_dataframes script for maintaining datatypes, more error prone on less clean datasets.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def push_dataframe_dtypes(self, data, table="raw_data", batchsize=500, | |
overwrite=False, fast_upload=False): | |
# if overwrite is true we auto remove any tables with same name | |
if overwrite: | |
# check for pre-existing table and delete if present | |
self.drop(table) | |
# convert pyodbc connection string into sqlalchemy friendly format | |
connection_str = urllib.parse.quote_plus(self.connection_str) | |
# sqlalchemy engine is required for insert here | |
engine = sqlalchemy.create_engine('mssql+pyodbc:///?odbc_connect' | |
f'={connection_str}') | |
method = None | |
# if fast_upload chosen, set method to this | |
if fast_upload: | |
method = 'multi' | |
# upload to SQL server | |
data.to_sql(table, engine, chunksize=batchsize, method=method) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment