1414from pandas .core .config import get_option , set_option
1515import pandas .core .common as com
1616import pandas .lib as lib
17- from pandas .tslib import iNaT , Timestamp , Timedelta
18-
17+ from pandas .tslib import iNaT , Timestamp , Timedelta , format_array_from_datetime
18+ from pandas .tseries .index import DatetimeIndex
19+ from pandas .tseries .period import PeriodIndex
1920import numpy as np
2021
2122import itertools
2223import csv
2324
24- from pandas .tseries .period import PeriodIndex , DatetimeIndex
25-
2625docstring_to_string = """
2726 Parameters
2827 ----------
@@ -1259,9 +1258,10 @@ def __init__(self, obj, path_or_buf=None, sep=",", na_rep='', float_format=None,
12591258 if isinstance (cols , Index ):
12601259 cols = cols .to_native_types (na_rep = na_rep ,
12611260 float_format = float_format ,
1262- date_format = date_format )
1261+ date_format = date_format ,
1262+ quoting = self .quoting )
12631263 else :
1264- cols = list (cols )
1264+ cols = np . asarray ( list (cols ) )
12651265 self .obj = self .obj .loc [:, cols ]
12661266
12671267 # update columns to include possible multiplicity of dupes
@@ -1270,9 +1270,10 @@ def __init__(self, obj, path_or_buf=None, sep=",", na_rep='', float_format=None,
12701270 if isinstance (cols , Index ):
12711271 cols = cols .to_native_types (na_rep = na_rep ,
12721272 float_format = float_format ,
1273- date_format = date_format )
1273+ date_format = date_format ,
1274+ quoting = self .quoting )
12741275 else :
1275- cols = list (cols )
1276+ cols = np . asarray ( list (cols ) )
12761277
12771278 # save it
12781279 self .cols = cols
@@ -1371,8 +1372,10 @@ def strftime_with_nulls(x):
13711372 values = self .obj .copy ()
13721373 values .index = data_index
13731374 values .columns = values .columns .to_native_types (
1374- na_rep = na_rep , float_format = float_format ,
1375- date_format = date_format )
1375+ na_rep = na_rep ,
1376+ float_format = float_format ,
1377+ date_format = date_format ,
1378+ quoting = self .quoting )
13761379 values = values [cols ]
13771380
13781381 series = {}
@@ -1543,18 +1546,22 @@ def _save_chunk(self, start_i, end_i):
15431546 slicer = slice (start_i , end_i )
15441547 for i in range (len (self .blocks )):
15451548 b = self .blocks [i ]
1546- d = b .to_native_types (slicer = slicer , na_rep = self .na_rep ,
1549+ d = b .to_native_types (slicer = slicer ,
1550+ na_rep = self .na_rep ,
15471551 float_format = self .float_format ,
15481552 decimal = self .decimal ,
1549- date_format = self .date_format )
1553+ date_format = self .date_format ,
1554+ quoting = self .quoting )
15501555
15511556 for col_loc , col in zip (b .mgr_locs , d ):
15521557 # self.data is a preallocated list
15531558 self .data [col_loc ] = col
15541559
1555- ix = data_index .to_native_types (slicer = slicer , na_rep = self .na_rep ,
1560+ ix = data_index .to_native_types (slicer = slicer ,
1561+ na_rep = self .na_rep ,
15561562 float_format = self .float_format ,
1557- date_format = self .date_format )
1563+ date_format = self .date_format ,
1564+ quoting = self .quoting )
15581565
15591566 lib .write_csv_rows (self .data , ix , self .nlevels , self .cols , self .writer )
15601567
@@ -2030,16 +2037,43 @@ def __init__(self, values, nat_rep='NaT', date_format=None, **kwargs):
20302037 self .date_format = date_format
20312038
20322039 def _format_strings (self ):
2033- formatter = (self .formatter or
2034- _get_format_datetime64_from_values (self .values ,
2035- nat_rep = self .nat_rep ,
2036- date_format = self .date_format ))
20372040
2038- fmt_values = [formatter (x ) for x in self .values ]
2041+ # we may have a tz, if so, then need to process element-by-element
2042+ # when DatetimeBlockWithTimezones is a reality this could be fixed
2043+ values = self .values
2044+ if not isinstance (values , DatetimeIndex ):
2045+ values = DatetimeIndex (values )
2046+
2047+ if values .tz is None :
2048+ fmt_values = format_array_from_datetime (values .asi8 .ravel (),
2049+ format = _get_format_datetime64_from_values (values , self .date_format ),
2050+ na_rep = self .nat_rep ).reshape (values .shape )
2051+ fmt_values = fmt_values .tolist ()
2052+
2053+ else :
2054+
2055+ values = values .asobject
2056+ is_dates_only = _is_dates_only (values )
2057+ formatter = (self .formatter or _get_format_datetime64 (is_dates_only , values , date_format = self .date_format ))
2058+ fmt_values = [ formatter (x ) for x in self .values ]
20392059
20402060 return fmt_values
20412061
20422062
2063+ def _is_dates_only (values ):
2064+ # return a boolean if we are only dates (and don't have a timezone)
2065+ values = DatetimeIndex (values )
2066+ if values .tz is not None :
2067+ return False
2068+
2069+ values_int = values .asi8
2070+ consider_values = values_int != iNaT
2071+ one_day_nanos = (86400 * 1e9 )
2072+ even_days = np .logical_and (consider_values , values_int % one_day_nanos != 0 ).sum () == 0
2073+ if even_days :
2074+ return True
2075+ return False
2076+
20432077def _format_datetime64 (x , tz = None , nat_rep = 'NaT' ):
20442078 if x is None or lib .checknull (x ):
20452079 return nat_rep
@@ -2062,22 +2096,6 @@ def _format_datetime64_dateonly(x, nat_rep='NaT', date_format=None):
20622096 else :
20632097 return x ._date_repr
20642098
2065-
2066- def _is_dates_only (values ):
2067- # return a boolean if we are only dates (and don't have a timezone)
2068- from pandas import DatetimeIndex
2069- values = DatetimeIndex (values )
2070- if values .tz is not None :
2071- return False
2072-
2073- values_int = values .asi8
2074- consider_values = values_int != iNaT
2075- one_day_nanos = (86400 * 1e9 )
2076- even_days = np .logical_and (consider_values , values_int % one_day_nanos != 0 ).sum () == 0
2077- if even_days :
2078- return True
2079- return False
2080-
20812099def _get_format_datetime64 (is_dates_only , nat_rep = 'NaT' , date_format = None ):
20822100
20832101 if is_dates_only :
@@ -2088,13 +2106,12 @@ def _get_format_datetime64(is_dates_only, nat_rep='NaT', date_format=None):
20882106 return lambda x , tz = None : _format_datetime64 (x , tz = tz , nat_rep = nat_rep )
20892107
20902108
2091- def _get_format_datetime64_from_values (values ,
2092- nat_rep = 'NaT' ,
2093- date_format = None ):
2109+ def _get_format_datetime64_from_values (values , date_format ):
2110+ """ given values and a date_format, return a string format """
20942111 is_dates_only = _is_dates_only (values )
2095- return _get_format_datetime64 ( is_dates_only = is_dates_only ,
2096- nat_rep = nat_rep ,
2097- date_format = date_format )
2112+ if is_dates_only :
2113+ return date_format or "%Y-%m-%d"
2114+ return None
20982115
20992116
21002117class Timedelta64Formatter (GenericArrayFormatter ):
0 commit comments