Skip to content

Commit 36de148

Browse files
asmirnov69Ubuntu
andauthored
[ENH] Add lazy imports to speed up the time taken to load pyjanitor (part 2) (#1180)
* plugged changes from commits from 3225b19 to 9d2732b(origin/feature/conditional-imports) to latest dev * added lazy_imports.md and tuna image * changes made in AUTHORS.md and CHANGELOG.md * __version__ set be 0.23.1, lazy_loader is added to .requirements/base.in Co-authored-by: Ubuntu <[email protected]>
1 parent cdf62da commit 36de148

File tree

12 files changed

+140
-43
lines changed

12 files changed

+140
-43
lines changed

.requirements/base.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ natsort
66
pandas_flavor
77
multipledispatch
88
scipy
9+
lazy_loader

AUTHORS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,4 @@ Contributors
109109
- [@ethompsy](https://github.com/ethompsy) | [contributions](https://github.com/pyjanitor-devs/pyjanitor/issues?q=is%3Aclosed+mentions%3Aethompsy)
110110
- [@apatao](https://github.com/apatao) | [contributions](https://github.com/pyjanitor-devs/pyjanitor/issues?q=is%3Aclosed+mentions%3Aapatao)
111111
- [@OdinTech3](https://github.com/OdinTech3) | [contributions](https://github.com/pyjanitor-devs/pyjanitor/pull/1094)
112+
- [@asmirnov69](https://github.com/asmirnov69) | [contributions](https://github.com/pyjanitor-devs/pyjanitor/issues/1059)

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
## [Unreleased]
44

5+
- [ENH] Add lazy imports to speed up the time taken to load pyjanitor (part 2)
56
- [DOC] Updated developer guide docs.
67
- [ENH] Allow column selection/renaming within conditional_join. Issue #1102. Also allow first or last match. Issue #1020 @samukweku.
78
- [ENH] New decorator `deprecated_kwargs` for breaking API. #1103 @Zeroto521

environment-dev.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,4 @@ dependencies:
5555
- xorg-libxrender
5656
- pip:
5757
- mknotebooks
58+
- lazy-loader

janitor/__init__.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
1-
"""Top-level janitor API lives here."""
2-
try:
3-
import janitor.xarray # noqa: F401
4-
except ImportError:
5-
pass
1+
"""Top-level janitor API lives here.
2+
3+
Lazy loading used here to speed up imports.
4+
"""
5+
import lazy_loader as lazy
66

7+
8+
from .accessors import * # noqa: F403, F401
79
from .functions import * # noqa: F403, F401
810
from .io import * # noqa: F403, F401
911
from .math import * # noqa: F403, F401
1012
from .ml import get_features_targets as _get_features_targets
1113
from .utils import refactored_function
12-
from .accessors import * # noqa: F403, F401
14+
15+
16+
_ = lazy.load("pandas_flavor")
17+
18+
try:
19+
jxr = lazy.load("janitor.xarray") # noqa: F401
20+
except ImportError:
21+
pass
1322

1423

1524
@refactored_function(

janitor/accessors/__init__.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,17 @@
1-
"""Top-level imports for pyjanitor's dataframe accessors."""
2-
from .data_description import DataDescription
1+
"""Miscellaneous mathematical operators.
2+
3+
Lazy loading used here to speed up imports.
4+
"""
5+
6+
import warnings
7+
from typing import Tuple
8+
9+
10+
import lazy_loader as lazy
11+
12+
scipy_special = lazy.load("scipy.special")
13+
ss = lazy.load("scipy.stats")
14+
pf = lazy.load("pandas_flavor")
15+
pd = lazy.load("pandas")
16+
np = lazy.load("numpy")
17+
pdtypes = lazy.load("pandas.api.types")

janitor/accessors/data_description.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import lazy_loader as lazy
12
from typing import Dict, List, Union
2-
import pandas_flavor as pf
3-
import pandas as pd
3+
4+
pf = lazy.load("pandas_flavor")
5+
pd = lazy.load("pandas")
46

57

68
@pf.register_dataframe_accessor("data_description")

janitor/functions/impute.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
"""Implementation of `impute` function"""
22
from typing import Any, Hashable, Optional
33

4+
import lazy_loader as lazy
45
import numpy as np
56
import pandas_flavor as pf
67
import pandas as pd
7-
from scipy.stats import mode
88

99
from janitor.utils import deprecated_alias
1010

11+
ss = lazy.load("scipy.stats")
12+
1113

1214
@pf.register_dataframe_method
1315
@deprecated_alias(column="column_name")
@@ -100,7 +102,7 @@ def impute(
100102
"mean": np.mean,
101103
"average": np.mean, # aliased
102104
"median": np.median,
103-
"mode": mode,
105+
"mode": ss.mode,
104106
"minimum": np.min,
105107
"min": np.min, # aliased
106108
"maximum": np.max,

janitor/math.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
1-
""" Miscellaneous mathematical operators. """
1+
"""Miscellaneous mathematical operators.
2+
3+
Lazy loading used here to speed up imports.
4+
"""
25

36
import warnings
47
from typing import Tuple
58

6-
import numpy as np
7-
import pandas as pd
8-
import pandas_flavor as pf
9-
from pandas.api.types import is_numeric_dtype
10-
from scipy.special import expit
11-
from scipy.special import logit as scipy_logit
12-
from scipy.special import softmax as scipy_softmax
13-
from scipy.stats import norm
9+
10+
import lazy_loader as lazy
11+
12+
scipy_special = lazy.load("scipy.special")
13+
ss = lazy.load("scipy.stats")
14+
pf = lazy.load("pandas_flavor")
15+
pd = lazy.load("pandas")
16+
np = lazy.load("numpy")
17+
pdtypes = lazy.load("pandas.api.types")
1418

1519

1620
@pf.register_series_method
@@ -94,7 +98,7 @@ def sigmoid(s: pd.Series) -> pd.Series:
9498
:param s: Input Series.
9599
:return: Transformed Series.
96100
"""
97-
return expit(s)
101+
return scipy_special.expit(s)
98102

99103

100104
@pf.register_series_method
@@ -125,7 +129,7 @@ def softmax(s: pd.Series) -> pd.Series:
125129
:return: Transformed Series.
126130
"""
127131

128-
return pd.Series(scipy_softmax(s), index=s.index, name=s.name)
132+
return pd.Series(scipy_special.softmax(s), index=s.index, name=s.name)
129133

130134

131135
@pf.register_series_method
@@ -168,7 +172,7 @@ def logit(s: pd.Series, error: str = "warn") -> pd.Series:
168172
else:
169173
pass
170174
s[outside_support] = np.nan
171-
return scipy_logit(s)
175+
return scipy_special.logit(s)
172176

173177

174178
@pf.register_series_method
@@ -188,7 +192,7 @@ def normal_cdf(s: pd.Series) -> pd.Series:
188192
:param s: Input Series.
189193
:return: Transformed Series.
190194
"""
191-
return pd.Series(norm.cdf(s), index=s.index)
195+
return pd.Series(ss.norm.cdf(s), index=s.index)
192196

193197

194198
@pf.register_series_method
@@ -229,7 +233,7 @@ def probit(s: pd.Series, error: str = "warn") -> pd.Series:
229233
pass
230234
s[outside_support] = np.nan
231235
with np.errstate(all="ignore"):
232-
out = pd.Series(norm.ppf(s), index=s.index)
236+
out = pd.Series(ss.norm.ppf(s), index=s.index)
233237
return out
234238

235239

@@ -313,7 +317,7 @@ def ecdf(s: pd.Series) -> Tuple[np.ndarray, np.ndarray]:
313317
:raises TypeError: if series is not numeric.
314318
:raises ValueError: if series contains nulls.
315319
"""
316-
if not is_numeric_dtype(s):
320+
if not pdtypes.is_numeric_dtype(s):
317321
raise TypeError(f"series {s.name} must be numeric!")
318322
if not s.isna().sum() == 0:
319323
raise ValueError(f"series {s.name} contains nulls. Please drop them.")

janitor/utils.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
1-
"""Miscellaneous internal PyJanitor helper functions."""
1+
"""Miscellaneous mathematical operators.
2+
3+
Lazy loading used here to speed up imports.
4+
"""
25

36
import os
4-
import socket
57
import sys
6-
from warnings import warn
7-
from functools import singledispatch, wraps
8+
import socket
89
from typing import Callable, Dict, Iterable, Union
10+
from functools import singledispatch, wraps
11+
from warnings import warn
12+
13+
import lazy_loader as lazy
914

10-
import numpy as np
11-
import pandas as pd
15+
scipy_special = lazy.load("scipy.special")
16+
ss = lazy.load("scipy.stats")
17+
pf = lazy.load("pandas_flavor")
18+
pd = lazy.load("pandas")
19+
np = lazy.load("numpy")
20+
pdtypes = lazy.load("pandas.api.types")
1221

1322

1423
def check(varname: str, value, expected_types: list):

0 commit comments

Comments
 (0)