Skip to content

Commit 7f3423c

Browse files
Alexander Nordinjreback
Alexander Nordin
authored andcommitted
ERR: better error message on too large excel sheet (#26080)
1 parent c6a7cc1 commit 7f3423c

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

doc/source/whatsnew/v0.25.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ I/O
533533
- Fixed memory leak in :meth:`DataFrame.to_json` when dealing with numeric data (:issue:`24889`)
534534
- Bug in :func:`read_json` where date strings with ``Z`` were not converted to a UTC timezone (:issue:`26168`)
535535
- Added ``cache_dates=True`` parameter to :meth:`read_csv`, which allows to cache unique dates when they are parsed (:issue:`25990`)
536+
- :meth:`DataFrame.to_excel` now raises a ``ValueError`` when the caller's dimensions exceed the limitations of Excel (:issue:`26051`)
536537

537538
Plotting
538539
^^^^^^^^

pandas/io/formats/excel.py

+10
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,9 @@ class ExcelFormatter:
341341
This is only called for body cells.
342342
"""
343343

344+
max_rows = 2**20
345+
max_cols = 2**14
346+
344347
def __init__(self, df, na_rep='', float_format=None, cols=None,
345348
header=True, index=True, index_label=None, merge_cells=False,
346349
inf_rep='inf', style_converter=None):
@@ -648,6 +651,13 @@ def write(self, writer, sheet_name='Sheet1', startrow=0,
648651
from pandas.io.excel import ExcelWriter
649652
from pandas.io.common import _stringify_path
650653

654+
num_rows, num_cols = self.df.shape
655+
if num_rows > self.max_rows or num_cols > self.max_cols:
656+
raise ValueError("This sheet is too large! Your sheet size is: " +
657+
"{}, {} ".format(num_rows, num_cols) +
658+
"Max sheet size is: {}, {}".
659+
format(self.max_rows, self.max_cols))
660+
651661
if isinstance(writer, ExcelWriter):
652662
need_save = False
653663
else:

pandas/tests/io/test_excel.py

+18
Original file line numberDiff line numberDiff line change
@@ -1118,6 +1118,24 @@ class and any subclasses, on account of the `autouse=True`
11181118
class TestExcelWriter(_WriterBase):
11191119
# Base class for test cases to run with different Excel writers.
11201120

1121+
def test_excel_sheet_size(self):
1122+
1123+
# GH 26080
1124+
breaking_row_count = 2**20 + 1
1125+
breaking_col_count = 2**14 + 1
1126+
# purposely using two arrays to prevent memory issues while testing
1127+
row_arr = np.zeros(shape=(breaking_row_count, 1))
1128+
col_arr = np.zeros(shape=(1, breaking_col_count))
1129+
row_df = pd.DataFrame(row_arr)
1130+
col_df = pd.DataFrame(col_arr)
1131+
1132+
msg = "sheet is too large"
1133+
with pytest.raises(ValueError, match=msg):
1134+
row_df.to_excel(self.path)
1135+
1136+
with pytest.raises(ValueError, match=msg):
1137+
col_df.to_excel(self.path)
1138+
11211139
def test_excel_sheet_by_name_raise(self, *_):
11221140
import xlrd
11231141

0 commit comments

Comments
 (0)