Skip to content

Commit 5138bdc

Browse files
author
locojaydev
committed
reader bug fix (colnames was None.1,....), datetime hadling, period
1 parent 9ae35f8 commit 5138bdc

File tree

1 file changed

+25
-6
lines changed

1 file changed

+25
-6
lines changed

pandas/io/parsers.py

+25-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import pandas.lib as lib
2323
import pandas._parser as _parser
24+
from pandas.tseries.period import Period
2425

2526
class DateConversionError(Exception):
2627
pass
@@ -970,6 +971,10 @@ def TextParser(*args, **kwds):
970971
# verbose=False, encoding=None, squeeze=False):
971972

972973

974+
def count_empty_vals(vals):
975+
return sum([1 for v in vals if v == '' or v is None])
976+
977+
973978
class PythonParser(ParserBase):
974979

975980
def __init__(self, f, **kwds):
@@ -1100,13 +1105,23 @@ def read(self, rows=None):
11001105
self.index_col,
11011106
self.index_names)
11021107

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+
11031115
alldata = self._rows_to_cols(content)
11041116
data = self._exclude_implicit_index(alldata)
11051117

11061118
columns, data = self._do_date_conversions(self.columns, data)
11071119

11081120
data = self._convert_data(data)
11091121
index = self._make_index(data, alldata, columns)
1122+
if indexnamerow:
1123+
coffset = len(indexnamerow) - len(columns)
1124+
index.names = indexnamerow[:coffset]
11101125

11111126
return index, columns, data
11121127

@@ -1870,7 +1885,8 @@ def sheet_names(self):
18701885

18711886
def _trim_excel_header(row):
18721887
# 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):
18741890
row = row[1:]
18751891
return row
18761892

@@ -1954,8 +1970,8 @@ def _conv_value(val):
19541970
val = int(val)
19551971
elif isinstance(val, np.bool8):
19561972
val = bool(val)
1957-
elif isinstance(val, lib.Timestamp):
1958-
val = val._repr_base
1973+
elif isinstance(val, Period):
1974+
val = "%s" % val
19591975

19601976
return val
19611977

@@ -1982,6 +1998,9 @@ def __init__(self, path):
19821998
else:
19831999
from openpyxl.workbook import Workbook
19842000
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])
19852004
self.path = path
19862005
self.sheets = {}
19872006
self.cur_sheet = None
@@ -2037,9 +2056,9 @@ def _writecells_xlsx(self, cells, sheet_name, startrow, startcol):
20372056
style.__getattribute__(field))
20382057

20392058
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"
20412060
elif isinstance(cell.val, datetime.date):
2042-
style.num_format_str = "YYYY-MM-DD"
2061+
xcell.style.number_format.format_code = "YYYY-MM-DD"
20432062

20442063
#merging requires openpyxl latest (works on 1.6.1)
20452064
#todo add version check
@@ -2063,7 +2082,7 @@ def _writecells_xls(self, cells, sheet_name, startrow, startcol):
20632082
val = _conv_value(cell.val)
20642083
style = CellStyleConverter.to_xls(cell.style)
20652084
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"
20672086
elif isinstance(val, datetime.date):
20682087
style.num_format_str = "YYYY-MM-DD"
20692088

0 commit comments

Comments
 (0)