@@ -516,8 +516,8 @@ def read_sql(sql, con, index_col=None, coerce_float=True, params=None,
516516
517517
518518def to_sql (frame , name , con , flavor = 'sqlite' , schema = None , if_exists = 'fail' ,
519- index = True , index_label = None , chunksize = None , dtype = None ,
520- indexes = None ):
519+ index = True , index_label = None , indexes = None , chunksize = None ,
520+ dtype = None ):
521521 """
522522 Write records stored in a DataFrame to a SQL database.
523523
@@ -548,6 +548,10 @@ def to_sql(frame, name, con, flavor='sqlite', schema=None, if_exists='fail',
548548 Column label for index column(s). If None is given (default) and
549549 `index` is True, then the index names are used.
550550 A sequence should be given if the DataFrame uses MultiIndex.
551+ indexes : list of column name(s). Columns names in this list will have
552+ an indexes created for them in the database.
553+
554+ .. versionadded:: 0.18.2
551555 chunksize : int, default None
552556 If not None, then rows will be written in batches of this size at a
553557 time. If None, all rows will be written at once.
@@ -852,9 +856,13 @@ def _index_name(self, index, index_label):
852856 return None
853857
854858 def _is_column_indexed (self , label ):
859+ # column is explicitly set to be indexed
855860 if self .indexes is not None and label in self .indexes :
856861 return True
857862
863+ # if df index is also a column it needs an index unless it's
864+ # also a primary key (otherwise there would be two indexes).
865+ # multi-index can use primary key if the left hand side matches.
858866 if self .index is not None and label in self .index :
859867 if self .keys is None :
860868 return True
0 commit comments