Skip to content

Commit

Permalink
Allow to_pandassql to accept SQLAlchemy Engine
Browse files Browse the repository at this point in the history
Modified the to_pandassql method to accept either a valid SQLAlchemy engine
string or a SQLAlchemy Engine instance. This is in response to a request on
GitHub.
  • Loading branch information
rnelsonchem committed Jan 26, 2016
1 parent 6aba1fc commit 8d9d071
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions simpledbf/simpledbf.py
Original file line number Diff line number Diff line change
Expand Up @@ -339,14 +339,14 @@ def _df_chunks(self, chunksize):
del(results)
yield df

def to_pandassql(self, engine_str, table=None, chunksize=None, na='nan'):
def to_pandassql(self, engine, table=None, chunksize=None, na='nan'):
'''Write DBF contents to an SQL database using Pandas.
Parameters
----------
engine_str : string
A SQLalchemy engine initialization string. See the SQL engine
dialect documentation for more information.
engine : SQLAlchemy Engine or string
A SQLalchemy Engine instance or an SQL initialization string. See
the SQL engine dialect documentation for more information.
table : string, optional
The name of the table to create for the DBF records. If 'None'
Expand Down Expand Up @@ -375,7 +375,15 @@ def to_pandassql(self, engine_str, table=None, chunksize=None, na='nan'):
self._na_set(na)
if not table:
table = self.dbf[:-4] # strip trailing ".dbf"
engine = sql.create_engine(engine_str)

if isinstance(engine, str):
engine_inst = sql.create_engine(engine)
elif isinstance(engine, sql.engine.Engine):
engine_inst = engine
else:
error = 'The engine argument is not a string or SQLAlchemy' +\
'engine.'
raise ValueError(error)

# Setup string types for proper length, otherwise Pandas assumes
# "Text" types, which may not be as efficient
Expand All @@ -389,10 +397,10 @@ def to_pandassql(self, engine_str, table=None, chunksize=None, na='nan'):
# The default behavior is to append new data to existing tables.
if not chunksize:
df = self.to_dataframe()
df.to_sql(table, engine, dtype=dtype, if_exists='append')
df.to_sql(table, engine_inst, dtype=dtype, if_exists='append')
else:
for df in self.to_dataframe(chunksize=chunksize):
df.to_sql(table, engine, dtype=dtype, if_exists='append')
df.to_sql(table, engine_inst, dtype=dtype, if_exists='append')
del(df)


Expand Down

0 comments on commit 8d9d071

Please sign in to comment.