15
15
import pandas .core .common as com
16
16
from pandas .compat import lzip , map , zip , raise_with_traceback , string_types
17
17
from pandas .core .api import DataFrame , Series
18
- from pandas .core .common import notnull
18
+ from pandas .core .common import notnull , isnull
19
19
from pandas .core .base import PandasObject
20
20
from pandas .tseries .tools import to_datetime
21
21
@@ -599,12 +599,6 @@ def create(self):
599
599
def insert_statement (self ):
600
600
return self .table .insert ()
601
601
602
- def maybe_asscalar (self , i ):
603
- try :
604
- return np .asscalar (i )
605
- except AttributeError :
606
- return i
607
-
608
602
def insert_data (self ):
609
603
if self .index is not None :
610
604
temp = self .frame .copy ()
@@ -617,17 +611,35 @@ def insert_data(self):
617
611
else :
618
612
temp = self .frame
619
613
620
- temp = temp .astype (object )
621
- temp = temp .where (notnull (temp ), None )
622
- return temp
614
+ column_names = list (map (str , temp .columns ))
615
+ ncols = len (column_names )
616
+ data_list = [None ] * ncols
617
+ blocks = temp ._data .blocks
618
+
619
+ for i in range (len (blocks )):
620
+ b = blocks [i ]
621
+ if b .is_datetime :
622
+ # convert to microsecond resolution so this yields datetime.datetime
623
+ d = b .values .astype ('M8[us]' ).astype (object )
624
+ else :
625
+ d = np .array (b .values , dtype = object )
626
+
627
+ # replace NaN with None
628
+ if b ._can_hold_na :
629
+ mask = isnull (d )
630
+ d [mask ] = None
631
+
632
+ for col_loc , col in zip (b .mgr_locs , d ):
633
+ data_list [col_loc ] = col
634
+
635
+ return column_names , data_list
623
636
624
637
def insert (self , chunksize = None ):
625
638
626
639
ins = self .insert_statement ()
627
- temp = self .insert_data ()
628
- keys = list (map (str , temp .columns ))
640
+ keys , data_list = self .insert_data ()
629
641
630
- nrows = len (temp )
642
+ nrows = len (self . frame )
631
643
if chunksize is None :
632
644
chunksize = nrows
633
645
chunks = int (nrows / chunksize ) + 1
@@ -639,12 +651,11 @@ def insert(self, chunksize=None):
639
651
end_i = min ((i + 1 ) * chunksize , nrows )
640
652
if start_i >= end_i :
641
653
break
642
- data_list = []
643
- for t in temp .iloc [start_i :end_i ].itertuples ():
644
- data = dict ((k , self .maybe_asscalar (v ))
645
- for k , v in zip (keys , t [1 :]))
646
- data_list .append (data )
647
- con .execute (ins , data_list )
654
+
655
+ chunk_list = [arr [start_i :end_i ] for arr in data_list ]
656
+ insert_list = [dict ((k , v ) for k , v in zip (keys , row ))
657
+ for row in zip (* chunk_list )]
658
+ con .execute (ins , insert_list )
648
659
649
660
def read (self , coerce_float = True , parse_dates = None , columns = None ):
650
661
@@ -1011,9 +1022,9 @@ def insert_statement(self):
1011
1022
def insert (self , chunksize = None ):
1012
1023
1013
1024
ins = self .insert_statement ()
1014
- temp = self .insert_data ()
1025
+ keys , data_list = self .insert_data ()
1015
1026
1016
- nrows = len (temp )
1027
+ nrows = len (self . frame )
1017
1028
if chunksize is None :
1018
1029
chunksize = nrows
1019
1030
chunks = int (nrows / chunksize ) + 1
@@ -1024,13 +1035,11 @@ def insert(self, chunksize=None):
1024
1035
end_i = min ((i + 1 ) * chunksize , nrows )
1025
1036
if start_i >= end_i :
1026
1037
break
1027
- data_list = []
1028
- for t in temp .iloc [start_i :end_i ].itertuples ():
1029
- data = tuple ((self .maybe_asscalar (v ) for v in t [1 :]))
1030
- data_list .append (data )
1031
-
1038
+ chunk_list = [arr [start_i :end_i ] for arr in data_list ]
1039
+ insert_list = [tuple ((v for v in row ))
1040
+ for row in zip (* chunk_list )]
1032
1041
cur = self .pd_sql .con .cursor ()
1033
- cur .executemany (ins , data_list )
1042
+ cur .executemany (ins , insert_list )
1034
1043
cur .close ()
1035
1044
1036
1045
def _create_table_setup (self ):
0 commit comments