From ad625338ee7dd85feadb03e15cc91a1f9de47693 Mon Sep 17 00:00:00 2001 From: trbs Date: Sun, 24 Apr 2016 17:27:31 +0200 Subject: [PATCH 1/3] gh9084_get_schema_index_parameter --- pandas/io/sql.py | 27 +++++++++++++++++++-------- pandas/io/tests/test_sql.py | 15 +++++++++++++++ 2 files changed, 34 insertions(+), 8 deletions(-) diff --git a/pandas/io/sql.py b/pandas/io/sql.py index 324988360c9fe..8910719d570cf 100644 --- a/pandas/io/sql.py +++ b/pandas/io/sql.py @@ -1299,9 +1299,10 @@ 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, - dtype=dtype) + def _create_sql_schema(self, frame, table_name, keys=None, dtype=None, + index=False, index_label=None): + table = SQLTable(table_name, self, frame=frame, index=index, keys=keys, + dtype=dtype, index_label=index_label) return str(table.sql_schema()) @@ -1683,13 +1684,14 @@ def drop_table(self, name, schema=None): drop_sql = "DROP TABLE %s" % escape(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, - keys=keys, dtype=dtype) + def _create_sql_schema(self, frame, table_name, keys=None, dtype=None, + index=False, index_label=None): + table = SQLiteTable(table_name, self, frame=frame, index=index, + keys=keys, dtype=dtype, index_label=index_label) return str(table.sql_schema()) -def get_schema(frame, name, flavor='sqlite', keys=None, con=None, dtype=None): +def get_schema(frame, name, flavor='sqlite', keys=None, con=None, dtype=None, index=False, index_label=None): """ Get the SQL db table schema for the given frame. @@ -1711,8 +1713,17 @@ def get_schema(frame, name, flavor='sqlite', 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 (for backwards compatibility) + Write DataFrame index as a column. + .. versionadded:: 0.18.1 + index_label : string or sequence, default None + Column label for index column(s). If None is given (default) and + `index` is True, then the index names are used. + A sequence should be given if the DataFrame uses MultiIndex. + .. versionadded:: 0.18.1 """ pandas_sql = pandasSQL_builder(con=con, flavor=flavor) - 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, index_label=index_label) diff --git a/pandas/io/tests/test_sql.py b/pandas/io/tests/test_sql.py index b72258cbf588d..1073ea11fb968 100644 --- a/pandas/io/tests/test_sql.py +++ b/pandas/io/tests/test_sql.py @@ -788,6 +788,21 @@ def test_get_schema_keys(self): constraint_sentence = 'CONSTRAINT test_pk PRIMARY KEY ("A", "B")' self.assertTrue(constraint_sentence in create_sql) + def test_get_schema_index(self): + # support index=True (GH9084) + frame = DataFrame({'col':[1,2,3]}, index=pd.date_range('2012-01-01', periods=3)) + create_sql = sql.get_schema(frame, 'test_schema', index=False) + self.assertFalse('index' in create_sql) + self.assertFalse('CREATE INDEX' in create_sql) + + create_sql = sql.get_schema(frame, 'test_schema', index=True) + self.assertTrue('index' in create_sql) + self.assertTrue('CREATE INDEX "ix_test_schema_index"ON "test_schema" ("index")' in create_sql) + + create_sql = sql.get_schema(frame, 'test_schema', index=True, index_label="idx") + self.assertTrue('idx' in create_sql) + self.assertTrue('CREATE INDEX "ix_test_schema_idx"ON "test_schema" ("idx")' in create_sql) + def test_chunksize_read(self): df = DataFrame(np.random.randn(22, 5), columns=list('abcde')) df.to_sql('test_chunksize', self.conn, index=False) From 28c538e0168c167522fa1e2d3da6e56dc2879772 Mon Sep 17 00:00:00 2001 From: trbs Date: Mon, 12 Sep 2016 00:24:31 -0400 Subject: [PATCH 2/3] fix flake8 errors --- pandas/io/sql.py | 3 ++- pandas/io/tests/test_sql.py | 12 ++++++++---- 2 files changed, 10 insertions(+), 5 deletions(-) diff --git a/pandas/io/sql.py b/pandas/io/sql.py index 57fb00332f283..0cf8dab579673 100644 --- a/pandas/io/sql.py +++ b/pandas/io/sql.py @@ -1527,7 +1527,8 @@ def _create_sql_schema(self, frame, table_name, keys=None, dtype=None, return str(table.sql_schema()) -def get_schema(frame, name, flavor=None, keys=None, con=None, dtype=None, index=False, index_label=None): +def get_schema(frame, name, flavor=None, keys=None, con=None, dtype=None, + index=False, index_label=None): """ Get the SQL db table schema for the given frame. diff --git a/pandas/io/tests/test_sql.py b/pandas/io/tests/test_sql.py index ce529ff48b356..82147ed8eed4b 100644 --- a/pandas/io/tests/test_sql.py +++ b/pandas/io/tests/test_sql.py @@ -787,18 +787,22 @@ def test_get_schema_keys(self): def test_get_schema_index(self): # support index=True (GH9084) - frame = DataFrame({'col':[1,2,3]}, index=pd.date_range('2012-01-01', periods=3)) + frame = DataFrame({'col': [1, 2, 3]}, + index=pd.date_range('2012-01-01', periods=3)) create_sql = sql.get_schema(frame, 'test_schema', index=False) self.assertFalse('index' in create_sql) self.assertFalse('CREATE INDEX' in create_sql) create_sql = sql.get_schema(frame, 'test_schema', index=True) self.assertTrue('index' in create_sql) - self.assertTrue('CREATE INDEX "ix_test_schema_index"ON "test_schema" ("index")' in create_sql) + self.assertTrue('CREATE INDEX "ix_test_schema_index" ' + 'ON "test_schema" ("index")' in create_sql) - create_sql = sql.get_schema(frame, 'test_schema', index=True, index_label="idx") + create_sql = sql.get_schema(frame, 'test_schema', index=True, + index_label="idx") self.assertTrue('idx' in create_sql) - self.assertTrue('CREATE INDEX "ix_test_schema_idx"ON "test_schema" ("idx")' in create_sql) + self.assertTrue('CREATE INDEX "ix_test_schema_idx" ' + 'ON "test_schema" ("idx")' in create_sql) def test_chunksize_read(self): df = DataFrame(np.random.randn(22, 5), columns=list('abcde')) From 679a79994413378b048717c6592dab52852a53b2 Mon Sep 17 00:00:00 2001 From: trbs Date: Wed, 14 Sep 2016 01:43:13 -0400 Subject: [PATCH 3/3] fix tests assertion (though the generated sql looks "wrong" without the space between index name and ON) --- pandas/io/tests/test_sql.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pandas/io/tests/test_sql.py b/pandas/io/tests/test_sql.py index 82147ed8eed4b..47437fe39900e 100644 --- a/pandas/io/tests/test_sql.py +++ b/pandas/io/tests/test_sql.py @@ -795,13 +795,13 @@ def test_get_schema_index(self): create_sql = sql.get_schema(frame, 'test_schema', index=True) self.assertTrue('index' in create_sql) - self.assertTrue('CREATE INDEX "ix_test_schema_index" ' + self.assertTrue('CREATE INDEX "ix_test_schema_index"' 'ON "test_schema" ("index")' in create_sql) create_sql = sql.get_schema(frame, 'test_schema', index=True, index_label="idx") self.assertTrue('idx' in create_sql) - self.assertTrue('CREATE INDEX "ix_test_schema_idx" ' + self.assertTrue('CREATE INDEX "ix_test_schema_idx"' 'ON "test_schema" ("idx")' in create_sql) def test_chunksize_read(self):