Skip to content

Commit 6f8baf2

Browse files
Added schema kwarg and test
Added what's new cleaned up code so that it follows flake8 Cleaned up after commit Removed print statement
1 parent a44ac34 commit 6f8baf2

File tree

3 files changed

+32
-7
lines changed

3 files changed

+32
-7
lines changed

doc/source/whatsnew/v1.1.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ Other enhancements
8888
- :class:`Series.str` now has a `fullmatch` method that matches a regular expression against the entire string in each row of the series, similar to `re.fullmatch` (:issue:`32806`).
8989
- :meth:`DataFrame.sample` will now also allow array-like and BitGenerator objects to be passed to ``random_state`` as seeds (:issue:`32503`)
9090
- :meth:`MultiIndex.union` will now raise `RuntimeWarning` if the object inside are unsortable, pass `sort=False` to suppress this warning (:issue:`33015`)
91+
- :meth:`get_schema` will now allow a schema kwarg that will add a schema into the create table statement
9192
-
9293

9394
.. ---------------------------------------------------------------------------

pandas/io/sql.py

+25-7
Original file line numberDiff line numberDiff line change
@@ -1372,9 +1372,15 @@ def drop_table(self, table_name, schema=None):
13721372
self.get_table(table_name, schema).drop()
13731373
self.meta.clear()
13741374

1375-
def _create_sql_schema(self, frame, table_name, keys=None, dtype=None):
1375+
def _create_sql_schema(self, frame, table_name, keys=None, dtype=None, schema=None):
13761376
table = SQLTable(
1377-
table_name, self, frame=frame, index=False, keys=keys, dtype=dtype
1377+
table_name,
1378+
self,
1379+
frame=frame,
1380+
index=False,
1381+
keys=keys,
1382+
dtype=dtype,
1383+
schema=schema,
13781384
)
13791385
return str(table.sql_schema())
13801386

@@ -1505,9 +1511,13 @@ def _create_table_setup(self):
15051511
create_tbl_stmts.append(
15061512
f"CONSTRAINT {self.name}_pk PRIMARY KEY ({cnames_br})"
15071513
)
1508-
1514+
if self.schema:
1515+
schema_to_add = self.schema + "."
1516+
else:
1517+
schema_to_add = ""
15091518
create_stmts = [
15101519
"CREATE TABLE "
1520+
+ schema_to_add
15111521
+ escape(self.name)
15121522
+ " (\n"
15131523
+ ",\n ".join(create_tbl_stmts)
@@ -1762,14 +1772,20 @@ def drop_table(self, name, schema=None):
17621772
drop_sql = f"DROP TABLE {_get_valid_sqlite_name(name)}"
17631773
self.execute(drop_sql)
17641774

1765-
def _create_sql_schema(self, frame, table_name, keys=None, dtype=None):
1775+
def _create_sql_schema(self, frame, table_name, keys=None, dtype=None, schema=None):
17661776
table = SQLiteTable(
1767-
table_name, self, frame=frame, index=False, keys=keys, dtype=dtype
1777+
table_name,
1778+
self,
1779+
frame=frame,
1780+
index=False,
1781+
keys=keys,
1782+
dtype=dtype,
1783+
schema=schema,
17681784
)
17691785
return str(table.sql_schema())
17701786

17711787

1772-
def get_schema(frame, name, keys=None, con=None, dtype=None):
1788+
def get_schema(frame, name, keys=None, con=None, dtype=None, schema=None):
17731789
"""
17741790
Get the SQL db table schema for the given frame.
17751791
@@ -1790,4 +1806,6 @@ def get_schema(frame, name, keys=None, con=None, dtype=None):
17901806
17911807
"""
17921808
pandas_sql = pandasSQL_builder(con=con)
1793-
return pandas_sql._create_sql_schema(frame, name, keys=keys, dtype=dtype)
1809+
return pandas_sql._create_sql_schema(
1810+
frame, name, keys=keys, dtype=dtype, schema=schema
1811+
)

pandas/tests/io/test_sql.py

+6
Original file line numberDiff line numberDiff line change
@@ -855,6 +855,12 @@ def test_get_schema(self):
855855
create_sql = sql.get_schema(self.test_frame1, "test", con=self.conn)
856856
assert "CREATE" in create_sql
857857

858+
def test_get_schema_with_schema(self):
859+
create_sql = sql.get_schema(
860+
self.test_frame1, "test", con=self.conn, schema="pypi"
861+
)
862+
assert "pypi" in create_sql
863+
858864
def test_get_schema_dtypes(self):
859865
float_frame = DataFrame({"a": [1.1, 1.2], "b": [2.1, 2.2]})
860866
dtype = sqlalchemy.Integer if self.mode == "sqlalchemy" else "INTEGER"

0 commit comments

Comments
 (0)