Skip to content

Commit 45a1fca

Browse files
committed
FIX: Fix docstring formatting, missing imports, and undefined variables
- Escape curly braces in to_excel docstring example to prevent KeyError - Add missing pandas import in test_xlsxwriter.py for MultiIndex usage - Add missing ExcelFormatter import in generic.py - Move ExcelWriter import out of TYPE_CHECKING block (used at runtime) - Remove undefined variables from ExcelWriter call - Fix line length in docstring
1 parent 59f2bc4 commit 45a1fca

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

pandas/core/generic.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -182,15 +182,17 @@
182182
Window,
183183
)
184184

185+
# Import ExcelFormatter at runtime since it's used in to_excel method
186+
from pandas.io.formats.excel import ExcelFormatter
185187
from pandas.io.formats.format import (
186188
DataFrameFormatter,
187189
DataFrameRenderer,
188190
)
189191
from pandas.io.formats.printing import pprint_thing
190192

191193
if TYPE_CHECKING:
192-
from collections.abc import Callable
193194
from collections.abc import (
195+
Callable,
194196
Hashable,
195197
Iterator,
196198
Mapping,
@@ -202,15 +204,17 @@
202204

203205
from pandas import (
204206
DataFrame,
205-
ExcelWriter,
206207
HDFStore,
207208
Series,
208209
)
209210
from pandas.core.indexers.objects import BaseIndexer
210211
from pandas.core.resample import Resampler
211212

213+
# Import ExcelWriter at runtime since it's used in to_excel method
212214
import textwrap
213215

216+
from pandas import ExcelWriter
217+
214218
# goal is to be able to define the docs close to function, while still being
215219
# able to share
216220
_shared_docs = {**_shared_docs}
@@ -773,6 +777,7 @@ def _set_axis(self, axis: AxisInt, labels: AnyArrayLike | list) -> None:
773777
"""
774778
labels = ensure_index(labels)
775779
self._mgr.set_axis(axis, labels)
780+
776781
@final
777782
@doc(klass=_shared_doc_kwargs["klass"])
778783
def droplevel(self, level: IndexLabel, axis: Axis = 0) -> Self:
@@ -1514,6 +1519,7 @@ def __bool__(self) -> NoReturn:
15141519
f"The truth value of a {type(self).__name__} is ambiguous. "
15151520
"Use a.empty, a.bool(), a.item(), a.any() or a.all()."
15161521
)
1522+
15171523
@final
15181524
def abs(self) -> Self:
15191525
"""
@@ -2265,7 +2271,7 @@ def to_excel(
22652271
22662272
Examples
22672273
--------
2268-
>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
2274+
>>> df = pd.DataFrame({{"A": [1, 2, 3], "B": [4, 5, 6]}})
22692275
>>> df.to_excel("pandas_simple.xlsx")
22702276
>>> df.to_excel("pandas_simple.xlsx", engine="openpyxl")
22712277
"""
@@ -2279,11 +2285,7 @@ def to_excel(
22792285
excel_writer = ExcelWriter(
22802286
excel_writer,
22812287
engine=engine,
2282-
mode=mode,
2283-
if_sheet_exists=if_sheet_exists,
22842288
engine_kwargs=engine_kwargs,
2285-
date_format=date_format,
2286-
datetime_format=datetime_format,
22872289
storage_options=storage_options,
22882290
)
22892291

@@ -5624,6 +5626,7 @@ def f(x) -> bool:
56245626
return self.loc(axis=axis)[values]
56255627
else:
56265628
raise TypeError("Must pass either `items`, `like`, or `regex`")
5629+
56275630
@final
56285631
def head(self, n: int = 5) -> Self:
56295632
"""
@@ -6101,7 +6104,8 @@ def __finalize__(self, other, method: str | None = None, **kwargs) -> Self:
61016104
----------
61026105
other : the object from which to get the attributes that we are going
61036106
to propagate. If ``other`` has an ``input_objs`` attribute, then
6104-
this attribute must contain an iterable of objects, each with an ``attrs`` attribute.
6107+
this attribute must contain an iterable of objects, each with an
6108+
``attrs`` attribute.
61056109
method : str, optional
61066110
A passed method name providing context on where ``__finalize__``
61076111
was called.
@@ -9706,6 +9710,7 @@ def align(
97069710
left = left.__finalize__(self)
97079711
right = right.__finalize__(other)
97089712
return left, right
9713+
97099714
@final
97109715
def _align_frame(
97119716
self,
@@ -12824,6 +12829,8 @@ def last_valid_index(self) -> Hashable:
1282412829
The required number of valid values to perform the operation. If fewer than
1282512830
``min_count`` non-NA values are present the result will be NA.
1282612831
"""
12832+
12833+
1282712834
def make_doc(name: str, ndim: int) -> str:
1282812835
"""
1282912836
Generate the docstring for a Series/DataFrame reduction.
@@ -13190,4 +13197,4 @@ def make_doc(name: str, ndim: int) -> str:
1319013197
examples=examples,
1319113198
**kwargs,
1319213199
)
13193-
return docstr
13200+
return docstr

pandas/tests/io/excel/test_xlsxwriter.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import pytest
55

6+
import pandas as pd
67
from pandas import DataFrame
78

89
from pandas.io.excel import ExcelWriter
@@ -179,4 +180,4 @@ def test_to_excel_autofilter_multiindex_no_merge_xlsxwriter(tmp_excel):
179180
assert ws.auto_filter is not None
180181
assert ws.auto_filter.ref is not None
181182
finally:
182-
wb.close()
183+
wb.close()

0 commit comments

Comments
 (0)