Skip to content

Commit 257e761

Browse files
authored
Updated Dataframe.to_excel and Dataframe.to_latex (#179)
* Updated to_excel and to_latex header which both accept a list of str * - moved to_latex and to_excel from module frame to generic - added openpyxl to dev-requirements - updated tests
1 parent 02e1748 commit 257e761

File tree

4 files changed

+33
-73
lines changed

4 files changed

+33
-73
lines changed

pandas-stubs/core/frame.pyi

Lines changed: 0 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -1986,25 +1986,6 @@ class DataFrame(NDFrame, OpsMixin):
19861986
errors: _str = ...,
19871987
storage_options: dict[_str, Any] | None = ...,
19881988
) -> _str: ...
1989-
def to_excel(
1990-
self,
1991-
excel_writer,
1992-
sheet_name: _str = ...,
1993-
na_rep: _str = ...,
1994-
float_format: _str | None = ...,
1995-
columns: _str | Sequence[_str] | None = ...,
1996-
header: _bool = ...,
1997-
index: _bool = ...,
1998-
index_label: _str | Sequence[_str] | None = ...,
1999-
startrow: int = ...,
2000-
startcol: int = ...,
2001-
engine: _str | None = ...,
2002-
merge_cells: _bool = ...,
2003-
encoding: _str | None = ...,
2004-
inf_rep: _str = ...,
2005-
verbose: _bool = ...,
2006-
freeze_panes: tuple[int, int] | None = ...,
2007-
) -> None: ...
20081989
def to_hdf(
20091990
self,
20101991
path_or_buf: FilePathOrBuffer,
@@ -2057,57 +2038,6 @@ class DataFrame(NDFrame, OpsMixin):
20572038
index: _bool = ...,
20582039
indent: int | None = ...,
20592040
) -> _str: ...
2060-
@overload
2061-
def to_latex(
2062-
self,
2063-
buf: FilePathOrBuffer | None,
2064-
columns: list[_str] | None = ...,
2065-
col_space: int | None = ...,
2066-
header: _bool = ...,
2067-
index: _bool = ...,
2068-
na_rep: _str = ...,
2069-
formatters=...,
2070-
float_format=...,
2071-
sparsify: _bool | None = ...,
2072-
index_names: _bool = ...,
2073-
bold_rows: _bool = ...,
2074-
column_format: _str | None = ...,
2075-
longtable: _bool | None = ...,
2076-
escape: _bool | None = ...,
2077-
encoding: _str | None = ...,
2078-
decimal: _str = ...,
2079-
multicolumn: _bool | None = ...,
2080-
multicolumn_format: _str | None = ...,
2081-
multirow: _bool | None = ...,
2082-
caption: _str | tuple[_str, _str] | None = ...,
2083-
label: _str | None = ...,
2084-
position: _str | None = ...,
2085-
) -> None: ...
2086-
@overload
2087-
def to_latex(
2088-
self,
2089-
columns: list[_str] | None = ...,
2090-
col_space: int | None = ...,
2091-
header: _bool = ...,
2092-
index: _bool = ...,
2093-
na_rep: _str = ...,
2094-
formatters=...,
2095-
float_format=...,
2096-
sparsify: _bool | None = ...,
2097-
index_names: _bool = ...,
2098-
bold_rows: _bool = ...,
2099-
column_format: _str | None = ...,
2100-
longtable: _bool | None = ...,
2101-
escape: _bool | None = ...,
2102-
encoding: _str | None = ...,
2103-
decimal: _str = ...,
2104-
multicolumn: _bool | None = ...,
2105-
multicolumn_format: _str | None = ...,
2106-
multirow: _bool | None = ...,
2107-
caption: _str | tuple[_str, _str] | None = ...,
2108-
label: _str | None = ...,
2109-
position: _str | None = ...,
2110-
) -> _str: ...
21112041
def to_pickle(
21122042
self,
21132043
path: _str,

pandas-stubs/core/generic.pyi

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ class NDFrame(PandasObject, indexing.IndexingMixin):
8383
na_rep: _str = ...,
8484
float_format: _str | None = ...,
8585
columns: _str | Sequence[_str] | None = ...,
86-
header: _bool = ...,
86+
header: _bool | list[_str] = ...,
8787
index: _bool = ...,
8888
index_label: _str | Sequence[_str] | None = ...,
8989
startrow: int = ...,
@@ -175,7 +175,7 @@ class NDFrame(PandasObject, indexing.IndexingMixin):
175175
buf: FilePathOrBuffer | None,
176176
columns: list[_str] | None = ...,
177177
col_space: int | None = ...,
178-
header: _bool = ...,
178+
header: _bool | list[_str] = ...,
179179
index: _bool = ...,
180180
na_rep: _str = ...,
181181
formatters=...,
@@ -200,7 +200,7 @@ class NDFrame(PandasObject, indexing.IndexingMixin):
200200
self,
201201
columns: list[_str] | None = ...,
202202
col_space: int | None = ...,
203-
header: _bool = ...,
203+
header: _bool | list[_str] = ...,
204204
index: _bool = ...,
205205
na_rep: _str = ...,
206206
formatters=...,

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ matplotlib = ">=3.3.2"
4646
pre-commit = ">=2.19.0"
4747
black = ">=22.6.0"
4848
isort = ">=5.10.1"
49+
openpyxl = ">=3.0.10"
4950

5051
[build-system]
5152
requires = ["poetry-core>=1.0.0"]

tests/test_frame.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1080,6 +1080,35 @@ def test_frame_getitem_isin() -> None:
10801080
check(assert_type(df[df.index.isin([1, 3, 5])], pd.DataFrame), pd.DataFrame)
10811081

10821082

1083+
def test_to_excel() -> None:
1084+
df = pd.DataFrame(data={"col1": [1, 2], "col2": [3, 4]})
1085+
1086+
with tempfile.NamedTemporaryFile(delete=False) as file:
1087+
df.to_excel(file.name, engine="openpyxl")
1088+
file.close()
1089+
df2: pd.DataFrame = pd.read_excel(file.name)
1090+
with tempfile.NamedTemporaryFile(delete=False) as file:
1091+
df.to_excel(Path(file.name), engine="openpyxl")
1092+
file.close()
1093+
df3: pd.DataFrame = pd.read_excel(file.name)
1094+
with tempfile.NamedTemporaryFile(delete=False) as file:
1095+
df.to_excel(file.name, engine="openpyxl", startrow=1, startcol=1, header=False)
1096+
file.close()
1097+
df4: pd.DataFrame = pd.read_excel(file.name)
1098+
with tempfile.NamedTemporaryFile(delete=False) as file:
1099+
df.to_excel(file.name, engine="openpyxl", sheet_name="sheet", index=False)
1100+
file.close()
1101+
df5: pd.DataFrame = pd.read_excel(file.name)
1102+
with tempfile.NamedTemporaryFile(delete=False) as file:
1103+
df.to_excel(file.name, engine="openpyxl", header=["x", "y"])
1104+
file.close()
1105+
df6: pd.DataFrame = pd.read_excel(file.name)
1106+
with tempfile.NamedTemporaryFile(delete=False) as file:
1107+
df.to_excel(file.name, engine="openpyxl", columns=["col1"])
1108+
file.close()
1109+
df7: pd.DataFrame = pd.read_excel(file.name)
1110+
1111+
10831112
def test_read_excel() -> None:
10841113
if TYPE_CHECKING: # skip pytest
10851114

0 commit comments

Comments
 (0)