Skip to content

Commit 44010db

Browse files
committed
FIX: Move ExcelWriter and ExcelFormatter imports to avoid circular import
- Keep ExcelWriter and ExcelFormatter in TYPE_CHECKING for type hints - Import both at runtime inside to_excel method to avoid circular import
1 parent d81840c commit 44010db

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

pandas/core/generic.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,6 @@
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
187185
from pandas.io.formats.format import (
188186
DataFrameFormatter,
189187
DataFrameRenderer,
@@ -204,17 +202,15 @@
204202

205203
from pandas import (
206204
DataFrame,
205+
ExcelWriter,
207206
HDFStore,
208207
Series,
209208
)
210209
from pandas.core.indexers.objects import BaseIndexer
211210
from pandas.core.resample import Resampler
212211

213-
# Import ExcelWriter at runtime since it's used in to_excel method
214212
import textwrap
215213

216-
from pandas import ExcelWriter
217-
218214
# goal is to be able to define the docs close to function, while still being
219215
# able to share
220216
_shared_docs = {**_shared_docs}
@@ -2275,6 +2271,9 @@ def to_excel(
22752271
>>> df.to_excel("pandas_simple.xlsx")
22762272
>>> df.to_excel("pandas_simple.xlsx", engine="openpyxl")
22772273
"""
2274+
# Import ExcelWriter here to avoid circular import
2275+
from pandas import ExcelWriter
2276+
22782277
if isinstance(excel_writer, ExcelWriter):
22792278
if engine is not None:
22802279
raise ValueError(
@@ -2289,6 +2288,9 @@ def to_excel(
22892288
storage_options=storage_options,
22902289
)
22912290

2291+
# Import ExcelFormatter here to avoid circular import
2292+
from pandas.io.formats.excel import ExcelFormatter
2293+
22922294
formatter = ExcelFormatter(
22932295
self,
22942296
na_rep=na_rep,

0 commit comments

Comments
 (0)