Skip to content

ENH: Support index=True for io.sql.get_schema #25030

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion doc/source/whatsnew/v0.24.1.rst
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ Fixed Regressions
Enhancements
^^^^^^^^^^^^


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you revert changes to this file?

.. _whatsnew_0241.bug_fixes:

Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v0.25.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ MultiIndex
I/O
^^^

-
- :func:`get_schema` now accepts an `index` parameter (default: `False`) that includes the index in the generated schema. (:issue:`9084`)
-
-

Expand Down
17 changes: 11 additions & 6 deletions pandas/io/sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -1223,8 +1223,9 @@ def drop_table(self, table_name, schema=None):
self.get_table(table_name, schema).drop()
self.meta.clear()

def _create_sql_schema(self, frame, table_name, keys=None, dtype=None):
table = SQLTable(table_name, self, frame=frame, index=False, keys=keys,
def _create_sql_schema(self, frame, table_name, keys=None, dtype=None,
index=False):
table = SQLTable(table_name, self, frame=frame, index=index, keys=keys,
dtype=dtype)
return str(table.sql_schema())

Expand Down Expand Up @@ -1565,13 +1566,14 @@ def drop_table(self, name, schema=None):
name=_get_valid_sqlite_name(name))
self.execute(drop_sql)

def _create_sql_schema(self, frame, table_name, keys=None, dtype=None):
table = SQLiteTable(table_name, self, frame=frame, index=False,
def _create_sql_schema(self, frame, table_name, keys=None, dtype=None,
index=False):
table = SQLiteTable(table_name, self, frame=frame, index=index,
keys=keys, dtype=dtype)
return str(table.sql_schema())


def get_schema(frame, name, keys=None, con=None, dtype=None):
def get_schema(frame, name, keys=None, con=None, dtype=None, index=False):
"""
Get the SQL db table schema for the given frame.

Expand All @@ -1589,8 +1591,11 @@ def get_schema(frame, name, keys=None, con=None, dtype=None):
dtype : dict of column name to SQL type, default None
Optional specifying the datatype for columns. The SQL type should
be a SQLAlchemy type, or a string for sqlite3 fallback connection.
index : boolean, default False
Whether to include DataFrame index as a column
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a period here at the end of the description?


"""

pandas_sql = pandasSQL_builder(con=con)
return pandas_sql._create_sql_schema(frame, name, keys=keys, dtype=dtype)
return pandas_sql._create_sql_schema(
frame, name, keys=keys, dtype=dtype, index=index)
15 changes: 15 additions & 0 deletions pandas/tests/io/test_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -823,6 +823,21 @@ def test_get_schema_keys(self):
constraint_sentence = 'CONSTRAINT test_pk PRIMARY KEY ("A", "B")'
assert constraint_sentence in create_sql

@pytest.mark.parametrize("index_arg, expected", [
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would be better if you simply parametrized on True and False and made "expected" the actual expected statement

({}, False),
({"index": False}, False),
({"index": True}, True),
])
def test_get_schema_with_index(self, index_arg, expected):
frame = DataFrame({
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call this df instead of frame

'one': pd.Series([1, 2, 3], index=['a', 'b', 'c']),
'two': pd.Series([1, 2, 3], index=['a', 'b', 'c'])
})
frame.index.name = 'alphabet'

create_sql = sql.get_schema(frame, 'test', con=self.conn, **index_arg)
assert ('alphabet' in create_sql) == expected

def test_chunksize_read(self):
df = DataFrame(np.random.randn(22, 5), columns=list('abcde'))
df.to_sql('test_chunksize', self.conn, index=False)
Expand Down