14
14
from pandas .core .config import get_option , set_option
15
15
import pandas .core .common as com
16
16
import 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
19
20
import numpy as np
20
21
21
22
import itertools
22
23
import csv
23
24
24
- from pandas .tseries .period import PeriodIndex , DatetimeIndex
25
-
26
25
docstring_to_string = """
27
26
Parameters
28
27
----------
@@ -2030,16 +2029,50 @@ def __init__(self, values, nat_rep='NaT', date_format=None, **kwargs):
2030
2029
self .date_format = date_format
2031
2030
2032
2031
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 ))
2037
2032
2038
- fmt_values = [formatter (x ) for x in self .values ]
2033
+ # we may have a tz, if so, then need to process element-by-element
2034
+ # when DatetimeBlockWithTimezones is a reality this could be fixed
2035
+ values = self .values
2036
+ if not isinstance (values , DatetimeIndex ):
2037
+ values = DatetimeIndex (values )
2038
+
2039
+ if values .tz is None :
2040
+
2041
+ is_dates_only = _is_dates_only (values )
2042
+ if is_dates_only :
2043
+ formatter = self .date_format or "%Y-%m-%d"
2044
+ else :
2045
+ formatter = None
2046
+
2047
+ fmt_values = format_array_from_datetime (values .asi8 .ravel (),
2048
+ format = formatter ,
2049
+ na_rep = self .nat_rep ).reshape (values .shape )
2050
+ fmt_values = fmt_values .tolist ()
2051
+
2052
+ else :
2053
+
2054
+ values = values .asobject
2055
+ is_dates_only = _is_dates_only (values )
2056
+ formatter = (self .formatter or _get_format_datetime64 (is_dates_only , values , date_format = self .date_format ))
2057
+ fmt_values = [ formatter (x ) for x in self .values ]
2039
2058
2040
2059
return fmt_values
2041
2060
2042
2061
2062
+ def _is_dates_only (values ):
2063
+ # return a boolean if we are only dates (and don't have a timezone)
2064
+ values = DatetimeIndex (values )
2065
+ if values .tz is not None :
2066
+ return False
2067
+
2068
+ values_int = values .asi8
2069
+ consider_values = values_int != iNaT
2070
+ one_day_nanos = (86400 * 1e9 )
2071
+ even_days = np .logical_and (consider_values , values_int % one_day_nanos != 0 ).sum () == 0
2072
+ if even_days :
2073
+ return True
2074
+ return False
2075
+
2043
2076
def _format_datetime64 (x , tz = None , nat_rep = 'NaT' ):
2044
2077
if x is None or lib .checknull (x ):
2045
2078
return nat_rep
@@ -2062,22 +2095,6 @@ def _format_datetime64_dateonly(x, nat_rep='NaT', date_format=None):
2062
2095
else :
2063
2096
return x ._date_repr
2064
2097
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
-
2081
2098
def _get_format_datetime64 (is_dates_only , nat_rep = 'NaT' , date_format = None ):
2082
2099
2083
2100
if is_dates_only :
@@ -2088,15 +2105,6 @@ def _get_format_datetime64(is_dates_only, nat_rep='NaT', date_format=None):
2088
2105
return lambda x , tz = None : _format_datetime64 (x , tz = tz , nat_rep = nat_rep )
2089
2106
2090
2107
2091
- def _get_format_datetime64_from_values (values ,
2092
- nat_rep = 'NaT' ,
2093
- date_format = None ):
2094
- 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 )
2098
-
2099
-
2100
2108
class Timedelta64Formatter (GenericArrayFormatter ):
2101
2109
2102
2110
def __init__ (self , values , nat_rep = 'NaT' , box = False , ** kwargs ):
0 commit comments