Skip to content

Commit 35e19b3

Browse files
committed
make all pandas imports lazy
1 parent 4d36811 commit 35e19b3

4 files changed

Lines changed: 23 additions & 9 deletions

File tree

src/gradient_free_optimizers/_memory.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,16 @@
33
# License: MIT License
44

55

6+
from __future__ import annotations
7+
68
import logging
79
from multiprocessing.managers import DictProxy
8-
from typing import Any
9-
10-
import pandas as pd
10+
from typing import TYPE_CHECKING, Any
1111

1212
from ._objective_adapter import ObjectiveAdapter
13+
14+
if TYPE_CHECKING:
15+
import pandas as pd
1316
from ._storage import BaseStorage, MemoryStorage
1417

1518

@@ -39,6 +42,8 @@ def memory(self, warm_start: pd.DataFrame, memory: Any = None):
3942
if warm_start is None:
4043
return
4144

45+
import pandas as pd
46+
4247
if not isinstance(warm_start, pd.DataFrame):
4348
logging.warning("Memory warm start must be of type pandas.DataFrame")
4449
logging.warning("Optimization will continue without memory warm start")

src/gradient_free_optimizers/_results_manager.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
from typing import TYPE_CHECKING, Any
44

5-
import pandas as pd
6-
75
if TYPE_CHECKING:
6+
import pandas as pd
7+
88
from .optimizers.core_optimizer.converter import Converter
99

1010

@@ -45,11 +45,12 @@ def dataframe(self) -> pd.DataFrame:
4545
This reconstructs parameter dictionaries only when needed,
4646
avoiding the memory cost of storing them during optimization.
4747
"""
48+
import pandas as pd
49+
4850
if not self._positions:
4951
return pd.DataFrame()
5052

5153
if self._converter is None:
52-
# Fallback: return just scores if no converter available
5354
return pd.DataFrame({"score": self._scores})
5455

5556
# Build rows lazily

src/gradient_free_optimizers/ask_tell/direct_algorithm.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,14 @@
33
# License: MIT License
44
"""DIRECT algorithm optimizer with ask/tell interface."""
55

6-
import pandas as pd
6+
from __future__ import annotations
7+
8+
from typing import TYPE_CHECKING
79

810
from .._ask_tell_mixin import AskTell
11+
12+
if TYPE_CHECKING:
13+
import pandas as pd
914
from ..optimizers import DirectAlgorithm as _DirectAlgorithm
1015

1116

src/gradient_free_optimizers/optimizer_search/direct_algorithm.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,14 @@
33
# License: MIT License
44
"""DIRECT algorithm using adaptive hyperrectangle subdivision."""
55

6-
from typing import Literal
6+
from __future__ import annotations
77

8-
import pandas as pd
8+
from typing import TYPE_CHECKING, Literal
99

1010
from .._init_utils import get_default_initialize
11+
12+
if TYPE_CHECKING:
13+
import pandas as pd
1114
from ..optimizers import DirectAlgorithm as _DirectAlgorithm
1215
from ..search import Search
1316

0 commit comments

Comments
 (0)