@@ -516,8 +516,8 @@ def read_sql(sql, con, index_col=None, coerce_float=True, params=None,
516
516
517
517
518
518
def 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 ):
521
521
"""
522
522
Write records stored in a DataFrame to a SQL database.
523
523
@@ -548,6 +548,10 @@ def to_sql(frame, name, con, flavor='sqlite', schema=None, if_exists='fail',
548
548
Column label for index column(s). If None is given (default) and
549
549
`index` is True, then the index names are used.
550
550
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
551
555
chunksize : int, default None
552
556
If not None, then rows will be written in batches of this size at a
553
557
time. If None, all rows will be written at once.
@@ -852,9 +856,13 @@ def _index_name(self, index, index_label):
852
856
return None
853
857
854
858
def _is_column_indexed (self , label ):
859
+ # column is explicitly set to be indexed
855
860
if self .indexes is not None and label in self .indexes :
856
861
return True
857
862
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.
858
866
if self .index is not None and label in self .index :
859
867
if self .keys is None :
860
868
return True
0 commit comments