From cfb60565b74aed7f99946b4ea306bda51859a4f4 Mon Sep 17 00:00:00 2001 From: Fangchen Li Date: Tue, 30 May 2023 22:34:33 -0500 Subject: [PATCH] CLN: use cache instead of lru_cache for unbound cache --- pandas/core/_numba/executor.py | 2 +- pandas/core/array_algos/take.py | 2 +- pandas/core/dtypes/cast.py | 2 +- pandas/core/groupby/numba_.py | 4 ++-- pandas/core/groupby/ops.py | 2 +- pandas/core/window/numba_.py | 10 +++++----- pandas/io/formats/excel.py | 11 ++++------- 7 files changed, 15 insertions(+), 18 deletions(-) diff --git a/pandas/core/_numba/executor.py b/pandas/core/_numba/executor.py index 891772602c442..b5a611560bde7 100644 --- a/pandas/core/_numba/executor.py +++ b/pandas/core/_numba/executor.py @@ -14,7 +14,7 @@ from pandas.compat._optional import import_optional_dependency -@functools.lru_cache(maxsize=None) +@functools.cache def generate_shared_aggregator( func: Callable[..., Scalar], nopython: bool, diff --git a/pandas/core/array_algos/take.py b/pandas/core/array_algos/take.py index 1e2f52a6a9413..30529ae62f56f 100644 --- a/pandas/core/array_algos/take.py +++ b/pandas/core/array_algos/take.py @@ -285,7 +285,7 @@ def take_2d_multi( return out -@functools.lru_cache(maxsize=128) +@functools.lru_cache def _get_take_nd_function_cached( ndim: int, arr_dtype: np.dtype, out_dtype: np.dtype, axis: AxisInt ): diff --git a/pandas/core/dtypes/cast.py b/pandas/core/dtypes/cast.py index d7e2631e92960..831b368f58225 100644 --- a/pandas/core/dtypes/cast.py +++ b/pandas/core/dtypes/cast.py @@ -586,7 +586,7 @@ def maybe_promote(dtype: np.dtype, fill_value=np.nan): return dtype, fill_value -@functools.lru_cache(maxsize=128) +@functools.lru_cache def _maybe_promote_cached(dtype, fill_value, fill_value_type): # The cached version of _maybe_promote below # This also use fill_value_type as (unused) argument to use this in the diff --git a/pandas/core/groupby/numba_.py b/pandas/core/groupby/numba_.py index 6bdb8d6b4d863..3b7a58e87603e 100644 --- a/pandas/core/groupby/numba_.py +++ b/pandas/core/groupby/numba_.py @@ -61,7 +61,7 @@ def f(values, index, ...): ) -@functools.lru_cache(maxsize=None) +@functools.cache def generate_numba_agg_func( func: Callable[..., Scalar], nopython: bool, @@ -121,7 +121,7 @@ def group_agg( return group_agg -@functools.lru_cache(maxsize=None) +@functools.cache def generate_numba_transform_func( func: Callable[..., np.ndarray], nopython: bool, diff --git a/pandas/core/groupby/ops.py b/pandas/core/groupby/ops.py index 0e280f15b9ea1..1cf27f37e469e 100644 --- a/pandas/core/groupby/ops.py +++ b/pandas/core/groupby/ops.py @@ -162,7 +162,7 @@ def get_kind_from_how(cls, how: str) -> str: # Note: we make this a classmethod and pass kind+how so that caching # works at the class level and not the instance level @classmethod - @functools.lru_cache(maxsize=None) + @functools.cache def _get_cython_function( cls, kind: str, how: str, dtype: np.dtype, is_numeric: bool ): diff --git a/pandas/core/window/numba_.py b/pandas/core/window/numba_.py index 71c7327665b65..9357945e78c63 100644 --- a/pandas/core/window/numba_.py +++ b/pandas/core/window/numba_.py @@ -17,7 +17,7 @@ from pandas._typing import Scalar -@functools.lru_cache(maxsize=None) +@functools.cache def generate_numba_apply_func( func: Callable[..., Scalar], nopython: bool, @@ -77,7 +77,7 @@ def roll_apply( return roll_apply -@functools.lru_cache(maxsize=None) +@functools.cache def generate_numba_ewm_func( nopython: bool, nogil: bool, @@ -175,7 +175,7 @@ def ewm( return ewm -@functools.lru_cache(maxsize=None) +@functools.cache def generate_numba_table_func( func: Callable[..., np.ndarray], nopython: bool, @@ -241,7 +241,7 @@ def roll_table( # This function will no longer be needed once numba supports # axis for all np.nan* agg functions # https://github.com/numba/numba/issues/1269 -@functools.lru_cache(maxsize=None) +@functools.cache def generate_manual_numpy_nan_agg_with_axis(nan_func): if TYPE_CHECKING: import numba @@ -259,7 +259,7 @@ def nan_agg_with_axis(table): return nan_agg_with_axis -@functools.lru_cache(maxsize=None) +@functools.cache def generate_numba_ewm_table_func( nopython: bool, nogil: bool, diff --git a/pandas/io/formats/excel.py b/pandas/io/formats/excel.py index 41e5c22447833..0dbb6529cd384 100644 --- a/pandas/io/formats/excel.py +++ b/pandas/io/formats/excel.py @@ -3,10 +3,7 @@ """ from __future__ import annotations -from functools import ( - lru_cache, - reduce, -) +import functools import itertools import re from typing import ( @@ -193,10 +190,10 @@ def __init__(self, inherited: str | None = None) -> None: self.inherited = self.compute_css(inherited) else: self.inherited = None - # We should avoid lru_cache on the __call__ method. + # We should avoid cache on the __call__ method. # Otherwise once the method __call__ has been called # garbage collection no longer deletes the instance. - self._call_cached = lru_cache(maxsize=None)(self._call_uncached) + self._call_cached = functools.cache(self._call_uncached) compute_css = CSSResolver() @@ -726,7 +723,7 @@ def _format_header(self) -> Iterable[ExcelCell]: row = [x if x is not None else "" for x in self.df.index.names] + [ "" ] * len(self.columns) - if reduce(lambda x, y: x and y, (x != "" for x in row)): + if functools.reduce(lambda x, y: x and y, (x != "" for x in row)): gen2 = ( ExcelCell(self.rowcounter, colindex, val, self.header_style) for colindex, val in enumerate(row)