-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
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
Changes from all commits
f012f07
3515ab4
09be25d
da48c3a
5707052
722dc56
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,6 @@ Fixed Regressions | |
Enhancements | ||
^^^^^^^^^^^^ | ||
|
||
|
||
.. _whatsnew_0241.bug_fixes: | ||
|
||
Bug Fixes | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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()) | ||
|
||
|
@@ -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): | ||
gfyoung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
""" | ||
Get the SQL db table schema for the given frame. | ||
|
||
|
@@ -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 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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", [ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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({ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
||
gfyoung marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def test_chunksize_read(self): | ||
df = DataFrame(np.random.randn(22, 5), columns=list('abcde')) | ||
df.to_sql('test_chunksize', self.conn, index=False) | ||
|
There was a problem hiding this comment.
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?