Skip to content

PERF/REF: make window/groupby imports lazy #51823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 17 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions pandas/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,9 @@
to_timedelta,
# misc
Flags,
Grouper,
factorize,
unique,
value_counts,
NamedAgg,
array,
Categorical,
set_eng_float_format,
Expand Down Expand Up @@ -184,6 +182,22 @@
del get_versions, v


def __getattr__(name: str):
# Lazify imports to speed "import pandas as pd"
if name in ("Grouper", "NamedAgg"):
from pandas.core import groupby

return getattr(groupby, name)
raise AttributeError(f"module 'pandas' has no attribute '{name}'")


def __dir__() -> list[str]:
# include lazy imports defined in __getattr__ in dir()
base = list(globals().keys())
result = base + ["Grouper", "NamedAgg"]
return result


# module level doc-string
__doc__ = """
pandas - a powerful data analysis and manipulation library for Python
Expand Down Expand Up @@ -243,7 +257,7 @@
"Flags",
"Float32Dtype",
"Float64Dtype",
"Grouper",
"Grouper", # pyright: ignore[reportUnsupportedDunderAll] # pylint: disable=undefined-all-variable # noqa:E501
"HDFStore",
"Index",
"IndexSlice",
Expand All @@ -257,7 +271,7 @@
"MultiIndex",
"NA",
"NaT",
"NamedAgg",
"NamedAgg", # pyright: ignore[reportUnsupportedDunderAll] # pylint: disable=undefined-all-variable # noqa:E501
"Period",
"PeriodDtype",
"PeriodIndex",
Expand Down
39 changes: 28 additions & 11 deletions pandas/core/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,10 @@
SettingWithCopyError,
SettingWithCopyWarning,
)
from pandas.util._decorators import doc
from pandas.util._decorators import (
Appender,
doc,
)
from pandas.util._exceptions import find_stack_level
from pandas.util._validators import (
check_dtype_backend,
Expand Down Expand Up @@ -177,14 +180,13 @@
find_valid_index,
)
from pandas.core.reshape.concat import concat
from pandas.core.shared_docs import _shared_docs
from pandas.core.sorting import get_indexer_indexer
from pandas.core.window import (
Expanding,
ExponentialMovingWindow,
Rolling,
Window,
from pandas.core.shared_docs import (
_shared_docs,
expanding_doc,
exponential_moving_window_doc,
window_doc,
)
from pandas.core.sorting import get_indexer_indexer

from pandas.io.formats.format import (
DataFrameFormatter,
Expand All @@ -203,6 +205,12 @@
)
from pandas.core.indexers.objects import BaseIndexer
from pandas.core.resample import Resampler
from pandas.core.window import (
Expanding,
ExponentialMovingWindow,
Rolling,
Window,
)

# goal is to be able to define the docs close to function, while still being
# able to share
Expand Down Expand Up @@ -11498,7 +11506,7 @@ def prod(
product = prod

@final
@doc(Rolling)
@Appender(window_doc)
def rolling(
self,
window: int | dt.timedelta | str | BaseOffset | BaseIndexer,
Expand Down Expand Up @@ -11533,6 +11541,11 @@ def rolling(
else:
axis = 0

from pandas.core.window import (
Rolling,
Window,
)

if win_type is not None:
return Window(
self,
Expand Down Expand Up @@ -11561,13 +11574,15 @@ def rolling(
)

@final
@doc(Expanding)
@Appender(expanding_doc)
def expanding(
self,
min_periods: int = 1,
axis: Axis | lib.NoDefault = lib.no_default,
method: str = "single",
) -> Expanding:
from pandas.core.window import Expanding

if axis is not lib.no_default:
axis = self._get_axis_number(axis)
name = "expanding"
Expand All @@ -11592,7 +11607,7 @@ def expanding(
return Expanding(self, min_periods=min_periods, axis=axis, method=method)

@final
@doc(ExponentialMovingWindow)
@Appender(exponential_moving_window_doc)
def ewm(
self,
com: float | None = None,
Expand All @@ -11606,6 +11621,8 @@ def ewm(
times: np.ndarray | DataFrame | Series | None = None,
method: str = "single",
) -> ExponentialMovingWindow:
from pandas.core.window import ExponentialMovingWindow

if axis is not lib.no_default:
axis = self._get_axis_number(axis)
name = "ewm"
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/groupby/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -673,7 +673,8 @@ def groups(self) -> dict[Hashable, np.ndarray]:
if len(self.groupings) == 1:
return self.groupings[0].groups
else:
to_groupby = zip(*(ping.grouping_vector for ping in self.groupings))
pings = (ping.grouping_vector for ping in self.groupings)
to_groupby = zip(*pings)
index = Index(to_groupby)
return self.axis.groupby(index)

Expand Down
5 changes: 4 additions & 1 deletion pandas/core/reshape/pivot.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@

import pandas.core.common as com
from pandas.core.frame import _shared_docs
from pandas.core.groupby import Grouper
from pandas.core.indexes.api import (
Index,
MultiIndex,
Expand Down Expand Up @@ -143,6 +142,8 @@ def __internal_pivot_table(
if i not in data:
raise KeyError(i)

from pandas.core.groupby import Grouper

to_filter = []
for x in keys + values:
if isinstance(x, Grouper):
Expand Down Expand Up @@ -492,6 +493,8 @@ def _all_key():


def _convert_by(by):
from pandas.core.groupby import Grouper

if by is None:
by = []
elif (
Expand Down
Loading