21
21
22
22
import pandas .lib as lib
23
23
import pandas ._parser as _parser
24
+ from pandas .tseries .period import Period
24
25
25
26
class DateConversionError (Exception ):
26
27
pass
@@ -970,6 +971,10 @@ def TextParser(*args, **kwds):
970
971
# verbose=False, encoding=None, squeeze=False):
971
972
972
973
974
+ def count_empty_vals (vals ):
975
+ return sum ([1 for v in vals if v == '' or v is None ])
976
+
977
+
973
978
class PythonParser (ParserBase ):
974
979
975
980
def __init__ (self , f , ** kwds ):
@@ -1100,13 +1105,23 @@ def read(self, rows=None):
1100
1105
self .index_col ,
1101
1106
self .index_names )
1102
1107
1108
+ #handle new style for names in index
1109
+ count_empty_content_vals = count_empty_vals (content [0 ])
1110
+ indexnamerow = None
1111
+ if count_empty_content_vals == len (columns ):
1112
+ indexnamerow = content [0 ]
1113
+ content = content [1 :]
1114
+
1103
1115
alldata = self ._rows_to_cols (content )
1104
1116
data = self ._exclude_implicit_index (alldata )
1105
1117
1106
1118
columns , data = self ._do_date_conversions (self .columns , data )
1107
1119
1108
1120
data = self ._convert_data (data )
1109
1121
index = self ._make_index (data , alldata , columns )
1122
+ if indexnamerow :
1123
+ coffset = len (indexnamerow ) - len (columns )
1124
+ index .names = indexnamerow [:coffset ]
1110
1125
1111
1126
return index , columns , data
1112
1127
@@ -1870,7 +1885,8 @@ def sheet_names(self):
1870
1885
1871
1886
def _trim_excel_header (row ):
1872
1887
# trim header row so auto-index inference works
1873
- while len (row ) > 0 and row [0 ] == '' :
1888
+ # xlrd uses '' , openpyxl None
1889
+ while len (row ) > 0 and (row [0 ] == '' or row [0 ] is None ):
1874
1890
row = row [1 :]
1875
1891
return row
1876
1892
@@ -1954,8 +1970,8 @@ def _conv_value(val):
1954
1970
val = int (val )
1955
1971
elif isinstance (val , np .bool8 ):
1956
1972
val = bool (val )
1957
- elif isinstance (val , lib . Timestamp ):
1958
- val = val . _repr_base
1973
+ elif isinstance (val , Period ):
1974
+ val = "%s" % val
1959
1975
1960
1976
return val
1961
1977
@@ -1982,6 +1998,9 @@ def __init__(self, path):
1982
1998
else :
1983
1999
from openpyxl .workbook import Workbook
1984
2000
self .book = Workbook ()#optimized_write=True)
2001
+ #open pyxl 1.6.1 adds a dummy sheet remove it
2002
+ if self .book .worksheets :
2003
+ self .book .remove_sheet (self .book .worksheets [0 ])
1985
2004
self .path = path
1986
2005
self .sheets = {}
1987
2006
self .cur_sheet = None
@@ -2037,9 +2056,9 @@ def _writecells_xlsx(self, cells, sheet_name, startrow, startcol):
2037
2056
style .__getattribute__ (field ))
2038
2057
2039
2058
if isinstance (cell .val , datetime .datetime ):
2040
- style .num_format_str = "YYYY-MM-DD HH:SS"
2059
+ xcell . style .number_format . format_code = "YYYY-MM-DD HH:MM :SS"
2041
2060
elif isinstance (cell .val , datetime .date ):
2042
- style .num_format_str = "YYYY-MM-DD"
2061
+ xcell . style .number_format . format_code = "YYYY-MM-DD"
2043
2062
2044
2063
#merging requires openpyxl latest (works on 1.6.1)
2045
2064
#todo add version check
@@ -2063,7 +2082,7 @@ def _writecells_xls(self, cells, sheet_name, startrow, startcol):
2063
2082
val = _conv_value (cell .val )
2064
2083
style = CellStyleConverter .to_xls (cell .style )
2065
2084
if isinstance (val , datetime .datetime ):
2066
- style .num_format_str = "YYYY-MM-DD HH:SS"
2085
+ style .num_format_str = "YYYY-MM-DD HH:MM: SS"
2067
2086
elif isinstance (val , datetime .date ):
2068
2087
style .num_format_str = "YYYY-MM-DD"
2069
2088
0 commit comments