Skip to content

Instantly share code, notes, and snippets.

@jamescalam
Created February 24, 2020 10:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamescalam/9532e8813f8161e66f420b0f427e7c87 to your computer and use it in GitHub Desktop.
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.
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