Skip to content

Moved freeze_panes validation to io/excel.py (#15160) #15592

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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