Skip to content

Commit

Permalink
CLN: Moved freeze_panes validation to io/excel.py (#15160)
Browse files Browse the repository at this point in the history
follow up to #15160

Author: Jeff Carey <jeff.carey@gmail.com>

Closes #15592 from jeffcarey/enh-15160-touchup2 and squashes the following commits:

81cb86f [Jeff Carey] Cleaned up freeze_panes validation code
a802fc7 [Jeff Carey] Moved freeze_panes validation to io/excel.py
  • Loading branch information
jeffcarey authored and jreback committed Mar 7, 2017
1 parent e097bf5 commit 1123982
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 15 deletions.
12 changes: 0 additions & 12 deletions pandas/core/frame.py
Original file line number Diff line number Diff line change
Expand Up @@ -1431,24 +1431,12 @@ def to_excel(self, excel_writer, sheet_name='Sheet1', na_rep='',
inf_rep=inf_rep)

formatted_cells = formatter.get_formatted_cells()
freeze_panes = self._validate_freeze_panes(freeze_panes)
excel_writer.write_cells(formatted_cells, sheet_name,
startrow=startrow, startcol=startcol,
freeze_panes=freeze_panes)
if need_save:
excel_writer.save()

def _validate_freeze_panes(self, freeze_panes):
if freeze_panes is not None:
if (
len(freeze_panes) == 2 and
all(isinstance(item, int) for item in freeze_panes)
):
return freeze_panes

raise ValueError("freeze_panes must be of form (row, column)"
" where row and column are integers")

def to_stata(self, fname, convert_dates=None, write_index=True,
encoding="latin-1", byteorder=None, time_stamp=None,
data_label=None, variable_labels=None):
Expand Down
22 changes: 19 additions & 3 deletions pandas/io/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -543,6 +543,22 @@ def __exit__(self, exc_type, exc_value, traceback):
self.close()


def _validate_freeze_panes(freeze_panes):
if freeze_panes is not None:
if (
len(freeze_panes) == 2 and
all(isinstance(item, int) for item in freeze_panes)
):
return True

raise ValueError("freeze_panes must be of form (row, column)"
" where row and column are integers")

# freeze_panes wasn't specified, return False so it won't be applied
# to output sheet
return False


def _trim_excel_header(row):
# trim header row so auto-index inference works
# xlrd uses '' , openpyxl None
Expand Down Expand Up @@ -1330,7 +1346,7 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,
wks.title = sheet_name
self.sheets[sheet_name] = wks

if freeze_panes is not None:
if _validate_freeze_panes(freeze_panes):
wks.freeze_panes = wks.cell(row=freeze_panes[0] + 1,
column=freeze_panes[1] + 1)

Expand Down Expand Up @@ -1418,7 +1434,7 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,
wks = self.book.add_sheet(sheet_name)
self.sheets[sheet_name] = wks

if freeze_panes is not None:
if _validate_freeze_panes(freeze_panes):
wks.set_panes_frozen(True)
wks.set_horz_split_pos(freeze_panes[0])
wks.set_vert_split_pos(freeze_panes[1])
Expand Down Expand Up @@ -1550,7 +1566,7 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,

style_dict = {}

if freeze_panes is not None:
if _validate_freeze_panes(freeze_panes):
wks.freeze_panes(*(freeze_panes))

for cell in cells:
Expand Down

0 comments on commit 1123982

Please sign in to comment.