@@ -484,16 +484,21 @@ def _try_coerce_and_cast_result(self, result, dtype=None):
484
484
def _try_fill (self , value ):
485
485
return value
486
486
487
- def to_native_types (self , slicer = None , na_rep = '' , ** kwargs ):
487
+ def to_native_types (self , slicer = None , na_rep = '' , quoting = None , ** kwargs ):
488
488
""" convert to our native types format, slicing if desired """
489
489
490
490
values = self .values
491
491
if slicer is not None :
492
492
values = values [:, slicer ]
493
- values = np .array (values , dtype = object )
494
493
mask = isnull (values )
494
+
495
+ if not self .is_object and not quoting :
496
+ values = values .astype (str )
497
+ else :
498
+ values = np .array (values , dtype = 'object' )
499
+
495
500
values [mask ] = na_rep
496
- return values . tolist ()
501
+ return values
497
502
498
503
# block actions ####
499
504
def copy (self , deep = True ):
@@ -1221,32 +1226,34 @@ def _try_cast(self, element):
1221
1226
return element
1222
1227
1223
1228
def to_native_types (self , slicer = None , na_rep = '' , float_format = None , decimal = '.' ,
1224
- ** kwargs ):
1229
+ quoting = None , ** kwargs ):
1225
1230
""" convert to our native types format, slicing if desired """
1226
1231
1227
1232
values = self .values
1228
1233
if slicer is not None :
1229
1234
values = values [:, slicer ]
1230
- values = np .array (values , dtype = object )
1231
1235
mask = isnull (values )
1232
- values [mask ] = na_rep
1233
-
1234
1236
1237
+ formatter = None
1235
1238
if float_format and decimal != '.' :
1236
1239
formatter = lambda v : (float_format % v ).replace ('.' ,decimal ,1 )
1237
1240
elif decimal != '.' :
1238
1241
formatter = lambda v : ('%g' % v ).replace ('.' ,decimal ,1 )
1239
1242
elif float_format :
1240
1243
formatter = lambda v : float_format % v
1244
+
1245
+ if formatter is None and not quoting :
1246
+ values = values .astype (str )
1241
1247
else :
1242
- formatter = None
1248
+ values = np . array ( values , dtype = 'object' )
1243
1249
1250
+ values [mask ] = na_rep
1244
1251
if formatter :
1245
1252
imask = (~ mask ).ravel ()
1246
1253
values .flat [imask ] = np .array (
1247
1254
[formatter (val ) for val in values .ravel ()[imask ]])
1248
1255
1249
- return values . tolist ()
1256
+ return values
1250
1257
1251
1258
def should_store (self , value ):
1252
1259
# when inserting a column should not coerce integers to floats
@@ -1366,7 +1373,7 @@ def _try_coerce_result(self, result):
1366
1373
def should_store (self , value ):
1367
1374
return issubclass (value .dtype .type , np .timedelta64 )
1368
1375
1369
- def to_native_types (self , slicer = None , na_rep = None , ** kwargs ):
1376
+ def to_native_types (self , slicer = None , na_rep = None , quoting = None , ** kwargs ):
1370
1377
""" convert to our native types format, slicing if desired """
1371
1378
1372
1379
values = self .values
@@ -1387,7 +1394,7 @@ def to_native_types(self, slicer=None, na_rep=None, **kwargs):
1387
1394
rvalues .flat [imask ] = np .array ([Timedelta (val )._repr_base (format = 'all' )
1388
1395
for val in values .ravel ()[imask ]],
1389
1396
dtype = object )
1390
- return rvalues . tolist ()
1397
+ return rvalues
1391
1398
1392
1399
1393
1400
def get_values (self , dtype = None ):
@@ -1763,18 +1770,19 @@ def _astype(self, dtype, copy=False, raise_on_error=True, values=None,
1763
1770
ndim = self .ndim ,
1764
1771
placement = self .mgr_locs )
1765
1772
1766
- def to_native_types (self , slicer = None , na_rep = '' , ** kwargs ):
1773
+ def to_native_types (self , slicer = None , na_rep = '' , quoting = None , ** kwargs ):
1767
1774
""" convert to our native types format, slicing if desired """
1768
1775
1769
1776
values = self .values
1770
1777
if slicer is not None :
1771
1778
# Categorical is always one dimension
1772
1779
values = values [slicer ]
1773
- values = np .array (values , dtype = object )
1774
1780
mask = isnull (values )
1781
+ values = np .array (values , dtype = 'object' )
1775
1782
values [mask ] = na_rep
1776
- # Blocks.to_native_type returns list of lists, but we are always only a list
1777
- return [values .tolist ()]
1783
+
1784
+ # we are expected to return a 2-d ndarray
1785
+ return values .reshape (1 ,len (values ))
1778
1786
1779
1787
class DatetimeBlock (Block ):
1780
1788
__slots__ = ()
@@ -1864,18 +1872,21 @@ def fillna(self, value, limit=None,
1864
1872
fastpath = True , placement = self .mgr_locs )]
1865
1873
1866
1874
def to_native_types (self , slicer = None , na_rep = None , date_format = None ,
1867
- ** kwargs ):
1875
+ quoting = None , ** kwargs ):
1868
1876
""" convert to our native types format, slicing if desired """
1869
1877
1870
1878
values = self .values
1871
1879
if slicer is not None :
1872
1880
values = values [:, slicer ]
1873
1881
1882
+ from pandas .core .format import _get_format_datetime64_from_values
1883
+ format = _get_format_datetime64_from_values (values , date_format )
1884
+
1874
1885
result = tslib .format_array_from_datetime (values .view ('i8' ).ravel (),
1875
1886
tz = None ,
1876
- format = date_format ,
1887
+ format = format ,
1877
1888
na_rep = na_rep ).reshape (values .shape )
1878
- return result . tolist ()
1889
+ return result
1879
1890
1880
1891
def should_store (self , value ):
1881
1892
return issubclass (value .dtype .type , np .datetime64 )
0 commit comments