Skip to content

Commit 109757c

Browse files
author
davidovitch
committed
read multiple sheets for ods, small PEP8 changes
1 parent 71e13b4 commit 109757c

File tree

1 file changed

+89
-62
lines changed

1 file changed

+89
-62
lines changed

pandas/io/excel.py

Lines changed: 89 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -433,7 +433,7 @@ def _parse_excel(self, sheetname=0, header=0, skiprows=None, skip_footer=0,
433433

434434
epoch1904 = self.book.datemode
435435

436-
def _parse_cell(cell_contents,cell_typ):
436+
def _parse_cell(cell_contents, cell_typ):
437437
"""converts the contents of the cell into a pandas
438438
appropriate object"""
439439

@@ -482,7 +482,7 @@ def _parse_cell(cell_contents,cell_typ):
482482

483483
ret_dict = False
484484

485-
#Keep sheetname to maintain backwards compatibility.
485+
# Keep sheetname to maintain backwards compatibility.
486486
if isinstance(sheetname, list):
487487
sheets = sheetname
488488
ret_dict = True
@@ -492,7 +492,7 @@ def _parse_cell(cell_contents,cell_typ):
492492
else:
493493
sheets = [sheetname]
494494

495-
#handle same-type duplicates.
495+
# handle same-type duplicates.
496496
sheets = list(set(sheets))
497497

498498
output = {}
@@ -517,7 +517,7 @@ def _parse_cell(cell_contents,cell_typ):
517517
should_parse[j] = self._should_parse(j, parse_cols)
518518

519519
if parse_cols is None or should_parse[j]:
520-
row.append(_parse_cell(value,typ))
520+
row.append(_parse_cell(value, typ))
521521
data.append(row)
522522

523523
if header is not None:
@@ -541,68 +541,95 @@ def _parse_cell(cell_contents,cell_typ):
541541
else:
542542
return output[asheetname]
543543

544-
545544
def _parse_ods(self, sheetname=0, header=0, skiprows=None, skip_footer=0,
546545
index_col=None, has_index_names=None, parse_cols=None,
547546
parse_dates=False, date_parser=None, na_values=None,
548547
thousands=None, chunksize=None, convert_float=True,
549-
**kwds):
550-
551-
# sheetname can be index or string
552-
sheet = self.book.sheets[sheetname]
553-
554-
data = []
555-
should_parse = {}
556-
for i in range(sheet.nrows()):
557-
row = []
558-
for j, cell in enumerate(sheet.row(i)):
559-
560-
if parse_cols is not None and j not in should_parse:
561-
should_parse[j] = self._should_parse(j, parse_cols)
562-
563-
if parse_cols is None or should_parse[j]:
564-
565-
if isinstance(cell.value, float):
566-
value = cell.value
567-
if convert_float:
568-
# GH5394 - Excel and ODS 'numbers' are always floats
569-
# it's a minimal perf hit and less suprising
570-
# FIXME: this goes wrong when int(cell.value) returns
571-
# a long (>1e18)
572-
val = int(cell.value)
573-
if val == cell.value:
574-
value = val
575-
elif isinstance(cell.value, compat.string_types):
576-
typ = cell.value_type
577-
# if typ == 'string':
578-
# value = cell.value
579-
if typ == 'date' or typ == 'time':
580-
value = self._parse_datetime(cell)
581-
else:
582-
value = cell.value
583-
elif isinstance(cell.value, bool):
584-
value = cell.value
585-
# elif isinstance(cell.value, type(None)):
586-
# value = np.nan
587-
else:
588-
value = np.nan
548+
verbose=False, **kwds):
549+
550+
def _parse_cell(cell):
551+
"""converts the contents of the cell into a pandas
552+
appropriate object"""
553+
if isinstance(cell.value, float):
554+
value = cell.value
555+
if convert_float:
556+
# GH5394 - Excel and ODS 'numbers' are always floats
557+
# it's a minimal perf hit and less suprising
558+
# FIXME: this goes wrong when int(cell.value) returns
559+
# a long (>1e18)
560+
val = int(cell.value)
561+
if val == cell.value:
562+
value = val
563+
elif isinstance(cell.value, compat.string_types):
564+
typ = cell.value_type
565+
# if typ == 'string':
566+
# value = cell.value
567+
if typ == 'date' or typ == 'time':
568+
value = self._parse_datetime(cell)
569+
else:
570+
value = cell.value
571+
elif isinstance(cell.value, bool):
572+
value = cell.value
573+
# elif isinstance(cell.value, type(None)):
574+
# value = np.nan
575+
else:
576+
value = np.nan
577+
return value
578+
579+
ret_dict = False
580+
581+
# Keep sheetname to maintain backwards compatibility.
582+
if isinstance(sheetname, list):
583+
sheets = sheetname
584+
ret_dict = True
585+
elif sheetname is None:
586+
sheets = self.sheet_names
587+
ret_dict = True
588+
else:
589+
sheets = [sheetname]
590+
591+
# handle same-type duplicates.
592+
sheets = list(set(sheets))
593+
594+
output = {}
595+
596+
for asheetname in sheets:
597+
if verbose:
598+
print("Reading sheet %s" % asheetname)
589599

590-
row.append(value)
600+
# sheetname can be index or string
601+
sheet = self.book.sheets[asheetname]
591602

592-
data.append(row)
603+
data = []
604+
should_parse = {}
605+
for i in range(sheet.nrows()):
606+
row = []
607+
for j, cell in enumerate(sheet.row(i)):
608+
609+
if parse_cols is not None and j not in should_parse:
610+
should_parse[j] = self._should_parse(j, parse_cols)
593611

594-
parser = TextParser(data, header=header, index_col=index_col,
595-
has_index_names=has_index_names,
596-
na_values=na_values,
597-
thousands=thousands,
598-
parse_dates=parse_dates,
599-
date_parser=date_parser,
600-
skiprows=skiprows,
601-
skip_footer=skip_footer,
602-
chunksize=chunksize,
603-
**kwds)
612+
if parse_cols is None or should_parse[j]:
613+
row.append(_parse_cell(cell))
614+
615+
data.append(row)
616+
617+
parser = TextParser(data, header=header, index_col=index_col,
618+
has_index_names=has_index_names,
619+
na_values=na_values,
620+
thousands=thousands,
621+
parse_dates=parse_dates,
622+
date_parser=date_parser,
623+
skiprows=skiprows,
624+
skip_footer=skip_footer,
625+
chunksize=chunksize,
626+
**kwds)
627+
output[asheetname] = parser.read()
604628

605-
return parser.read()
629+
if ret_dict:
630+
return output
631+
else:
632+
return output[asheetname]
606633

607634
def _parse_datetime(self, cell):
608635
"""Parse the date or time from on ods cell to a datetime object.
@@ -616,7 +643,7 @@ def _parse_datetime(self, cell):
616643
def _value2date(value):
617644
try:
618645
return datetime.datetime.strptime(value, '%Y-%m-%d')
619-
except ValueError:#, TypeError):
646+
except ValueError: # , TypeError):
620647
return datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S')
621648

622649
# Technically it is not necessary to try to derive the date/time
@@ -650,7 +677,7 @@ def _value2date(value):
650677
value = _value2date(cell.value)
651678
elif cell.value_type == 'time':
652679
try:
653-
# FIXME: what if the decimal separator is a comma in the locale?
680+
# FIXME: what if the decimal separator is a comma in locale?
654681
value = datetime.datetime.strptime(cell.value, 'PT%HH%MM%S.%fS')
655682
except ValueError:
656683
value = datetime.datetime.strptime(cell.value, 'PT%HH%MM%SS')
@@ -664,9 +691,9 @@ def _print_ods_cellinfo(self, cell):
664691
Cell attributes are documented here:
665692
https://pythonhosted.org/ezodf/tableobjects.html#id2
666693
"""
667-
print(' plaintext:', cell.plaintext()) # no formatting
694+
print(' plaintext:', cell.plaintext()) # no formatting
668695
# formatted, but what is difference with value?
669-
print('display_form:', cell.display_form) # format, ?=plaintext
696+
print('display_form:', cell.display_form) # format, ?=plaintext
670697
print(' value:', cell.value) # data handled
671698
print(' value_type:', cell.value_type) # data type
672699
print(' formula:', cell.formula)

0 commit comments

Comments
 (0)