Skip to content

ERR: better error message on too large excel sheet #26080

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

Merged
merged 17 commits into from
Jun 1, 2019
Merged
1 change: 1 addition & 0 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -2146,6 +2146,7 @@ def to_excel(self, excel_writer, sheet_name="Sheet1", na_rep="",
index_label=None, startrow=0, startcol=0, engine=None,
merge_cells=True, encoding=None, inf_rep="inf", verbose=True,
freeze_panes=None):

df = self if isinstance(self, ABCDataFrame) else self.to_frame()

from pandas.io.formats.excel import ExcelFormatter
Expand Down
9 changes: 9 additions & 0 deletions pandas/io/formats/excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,9 @@ class ExcelFormatter(object):
This is only called for body cells.
"""

max_rows = 2**20
max_cols = 2**14

def __init__(self, df, na_rep='', float_format=None, cols=None,
header=True, index=True, index_label=None, merge_cells=False,
inf_rep='inf', style_converter=None):
Expand Down Expand Up @@ -647,6 +650,12 @@ def write(self, writer, sheet_name='Sheet1', startrow=0,
"""
from pandas.io.excel import ExcelWriter
from pandas.io.common import _stringify_path

num_rows, num_cols = self.df.shape
if num_rows > self.max_rows or num_cols > self.max_cols:
raise ValueError("This sheet is too large! Your sheet size is: " +
"{}, {} ".format(num_rows, num_cols) +
"Max sheet size is: {}, {}".format(excel_max_rows, excel_max_cols))

if isinstance(writer, ExcelWriter):
need_save = False
Expand Down
11 changes: 11 additions & 0 deletions pandas/tests/io/test_excel.py
Original file line number Diff line number Diff line change
Expand Up @@ -1212,6 +1212,17 @@ class and any subclasses, on account of the `autouse=True`
class TestExcelWriter(_WriterBase):
# Base class for test cases to run with different Excel writers.

# def test_excel_sheet_size(self):
# assert False

# BREAKING_SHAPE = (2**20 + 1, 2**14 + 1)
# arr = np.zeros(shape=BREAKING_SHAPE)
# df = pd.DataFrame(arr)
# filepath = 'test.xlsx'

# with pytest.raises(ValueError):
# df.to_excel(filepath)

def test_excel_sheet_by_name_raise(self, *_):
import xlrd

Expand Down