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 1 commit
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
33 changes: 27 additions & 6 deletions pandas/io/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1331,8 +1331,15 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,
self.sheets[sheet_name] = wks

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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so what I meant originally, is you can add a _validate_freeze_panes method HERE (to avoid duplicating this code)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have any preference on whether _validate_freeze_panes is a method of the ExcelFile class or just a loose function in excel.py? Looking at other _validate functions in the code base it seems it can go either way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jeffcarey no preference. would be nice to have them be the same (could be a PR if you want). or even could do here. if you want.

len(freeze_panes) == 2 and
all(isinstance(item, int) for item in freeze_panes)
):
wks.freeze_panes = wks.cell(row=freeze_panes[0] + 1,
column=freeze_panes[1] + 1)
else:
raise ValueError("freeze_panes must be of form (row, column)"
" where row and column are integers")

for cell in cells:
xcell = wks.cell(
Expand Down Expand Up @@ -1419,9 +1426,16 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,
self.sheets[sheet_name] = wks

if freeze_panes is not None:
wks.set_panes_frozen(True)
wks.set_horz_split_pos(freeze_panes[0])
wks.set_vert_split_pos(freeze_panes[1])
if (
len(freeze_panes) == 2 and
all(isinstance(item, int) for item in freeze_panes)
):
wks.set_panes_frozen(True)
wks.set_horz_split_pos(freeze_panes[0])
wks.set_vert_split_pos(freeze_panes[1])
else:
raise ValueError("freeze_panes must be of form (row, column)"
" where row and column are integers")

style_dict = {}

Expand Down Expand Up @@ -1551,7 +1565,14 @@ def write_cells(self, cells, sheet_name=None, startrow=0, startcol=0,
style_dict = {}

if freeze_panes is not None:
wks.freeze_panes(*(freeze_panes))
if (
len(freeze_panes) == 2 and
all(isinstance(item, int) for item in freeze_panes)
):
wks.freeze_panes(*(freeze_panes))
else:
raise ValueError("freeze_panes must be of form (row, column)"
" where row and column are integers")

for cell in cells:
val = _conv_value(cell.val)
Expand Down