Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Specifying the schema in Pandas to_sql

From the source of to_sql, I can see that it gets mapped to an Meta Data object meta = MetaData(con, schema=schema). However, I can't find SQLAlchemy docs that tell me how to define the Schema for MySQL

How do I specify the schema string ?

like image 353
HackToHell Avatar asked Jun 23 '15 05:06

HackToHell


People also ask

What is schema in To_sql?

Function Parameters name – refers to the SQL table in which the data is saved. con – refers to the connection to the database engine. The SQLAlchemy engine manages the connection to the database. Hence, you can use any database supported by the library. schema – specifies the target schema under which to save the data.

What is DF To_sql?

DataFrame - to_sql() function. The to_sql() function is used to write records stored in a DataFrame to a SQL database. Syntax: DataFrame.to_sql(self, name, con, schema=None, if_exists='fail', index=True, index_label=None, chunksize=None, dtype=None, method=None)

Does pandas use SQLAlchemy?

Pandas in Python uses a module known as SQLAlchemy to connect to various databases and perform database operations.


1 Answers

The schema parameter in to_sql is confusing as the word "schema" means something different from the general meaning of "table definitions". In some SQL flavors, notably postgresql, a schema is effectively a namespace for a set of tables.

For example, you might have two schemas, one called test and one called prod. Each might contain a table called user_rankings generated in pandas and written using the to_sql command. You would specify the test schema when working on improvements to user rankings. When you are ready to deploy the new rankings, you would write to the prod schema.

As others have mentioned, when you call to_sql the table definition is generated from the type information for each column in the dataframe. If the table already exists in the database with exactly the same structure, you can use the append option to add new data to the table.

like image 138
Eilif Mikkelsen Avatar answered Sep 17 '22 22:09

Eilif Mikkelsen