Skip to content

Commit 1365c21

Browse files
committed
chore: Aded input validation to ensue that column index > 0
1 parent d1b05e8 commit 1365c21

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

pandas/io/formats/excel.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -880,8 +880,26 @@ def get_formatted_cells(self) -> Iterable[ExcelCell]:
880880
def _num2excel(self, index: int) -> str:
881881
"""
882882
Convert 0-based column index to Excel column name.
883+
884+
Parameters
885+
----------
886+
index : int
887+
The numeric column index to convert to a Excel column name.
888+
889+
Returns
890+
-------
891+
column_name : str
892+
The column name corresponding to the index.
893+
894+
Raises
895+
------
896+
ValueError
897+
Index is negative
883898
"""
899+
if index < 0:
900+
raise ValueError(f"Index cannot be negative: {index}")
884901
column_name = ""
902+
# while loop in case column name needs to be longer than 1 character
885903
while index > 0 or not column_name:
886904
index, remainder = divmod(index, 26)
887905
column_name = chr(65 + remainder) + column_name

0 commit comments

Comments
 (0)