Skip to content

Commit 12513c4

Browse files
Revert "ENH: Add method='table' for EWM.mean (#42273)" (#42302)
This reverts commit bba53fc.
1 parent 17ecb56 commit 12513c4

File tree

6 files changed

+7
-127
lines changed

6 files changed

+7
-127
lines changed

asv_bench/benchmarks/rolling.py

-3
Original file line numberDiff line numberDiff line change
@@ -296,8 +296,5 @@ def time_apply(self, method):
296296
table_method_func, raw=True, engine="numba"
297297
)
298298

299-
def time_ewm_mean(self, method):
300-
self.df.ewm(1, method=method).mean(engine="numba")
301-
302299

303300
from .pandas_vb_common import setup # noqa: F401 isort:skip

doc/source/whatsnew/v1.3.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ For example:
240240
Other enhancements
241241
^^^^^^^^^^^^^^^^^^
242242

243-
- :meth:`DataFrame.rolling`, :meth:`Series.rolling`, :meth:`DataFrame.expanding`, :meth:`Series.ewm`, :meth:`DataFrame.ewm`, :meth:`Series.expanding` now support a ``method`` argument with a ``'table'`` option that performs the windowing operation over an entire :class:`DataFrame`. See :ref:`Window Overview <window.overview>` for performance and functional benefits (:issue:`15095`, :issue:`38995`, :issue:`42273`)
243+
- :meth:`DataFrame.rolling`, :meth:`Series.rolling`, :meth:`DataFrame.expanding`, and :meth:`Series.expanding` now support a ``method`` argument with a ``'table'`` option that performs the windowing operation over an entire :class:`DataFrame`. See :ref:`Window Overview <window.overview>` for performance and functional benefits (:issue:`15095`, :issue:`38995`)
244244
- :class:`.ExponentialMovingWindow` now support a ``online`` method that can perform ``mean`` calculations in an online fashion. See :ref:`Window Overview <window.overview>` (:issue:`41673`)
245245
- Added :meth:`MultiIndex.dtypes` (:issue:`37062`)
246246
- Added ``end`` and ``end_day`` options for the ``origin`` argument in :meth:`DataFrame.resample` (:issue:`37804`)

pandas/core/generic.py

-2
Original file line numberDiff line numberDiff line change
@@ -10904,7 +10904,6 @@ def ewm(
1090410904
ignore_na: bool_t = False,
1090510905
axis: Axis = 0,
1090610906
times: str | np.ndarray | FrameOrSeries | None = None,
10907-
method: str = "single",
1090810907
) -> ExponentialMovingWindow:
1090910908
axis = self._get_axis_number(axis)
1091010909
# error: Value of type variable "FrameOrSeries" of "ExponentialMovingWindow"
@@ -10920,7 +10919,6 @@ def ewm(
1092010919
ignore_na=ignore_na,
1092110920
axis=axis,
1092210921
times=times,
10923-
method=method,
1092410922
)
1092510923

1092610924
# ----------------------------------------------------------------------

pandas/core/window/ewm.py

+6-28
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,7 @@
4040
ExponentialMovingWindowIndexer,
4141
GroupbyIndexer,
4242
)
43-
from pandas.core.window.numba_ import (
44-
generate_ewma_numba_table_func,
45-
generate_numba_ewma_func,
46-
)
43+
from pandas.core.window.numba_ import generate_numba_ewma_func
4744
from pandas.core.window.online import (
4845
EWMMeanState,
4946
generate_online_numba_ewma_func,
@@ -203,16 +200,6 @@ class ExponentialMovingWindow(BaseWindow):
203200
If 1-D array like, a sequence with the same shape as the observations.
204201
205202
Only applicable to ``mean()``.
206-
method : str {'single', 'table'}, default 'single'
207-
Execute the rolling operation per single column or row (``'single'``)
208-
or over the entire object (``'table'``).
209-
210-
This argument is only implemented when specifying ``engine='numba'``
211-
in the method call.
212-
213-
Only applicable to ``mean()``
214-
215-
.. versionadded:: 1.3.0
216203
217204
Returns
218205
-------
@@ -271,7 +258,6 @@ class ExponentialMovingWindow(BaseWindow):
271258
"ignore_na",
272259
"axis",
273260
"times",
274-
"method",
275261
]
276262

277263
def __init__(
@@ -286,7 +272,6 @@ def __init__(
286272
ignore_na: bool = False,
287273
axis: Axis = 0,
288274
times: str | np.ndarray | FrameOrSeries | None = None,
289-
method: str = "single",
290275
*,
291276
selection=None,
292277
):
@@ -296,7 +281,7 @@ def __init__(
296281
on=None,
297282
center=False,
298283
closed=None,
299-
method=method,
284+
method="single",
300285
axis=axis,
301286
selection=selection,
302287
)
@@ -452,19 +437,12 @@ def aggregate(self, func, *args, **kwargs):
452437
)
453438
def mean(self, *args, engine=None, engine_kwargs=None, **kwargs):
454439
if maybe_use_numba(engine):
455-
if self.method == "single":
456-
ewma_func = generate_numba_ewma_func(
457-
engine_kwargs, self._com, self.adjust, self.ignore_na, self._deltas
458-
)
459-
numba_cache_key = (lambda x: x, "ewma")
460-
else:
461-
ewma_func = generate_ewma_numba_table_func(
462-
engine_kwargs, self._com, self.adjust, self.ignore_na, self._deltas
463-
)
464-
numba_cache_key = (lambda x: x, "ewma_table")
440+
ewma_func = generate_numba_ewma_func(
441+
engine_kwargs, self._com, self.adjust, self.ignore_na, self._deltas
442+
)
465443
return self._apply(
466444
ewma_func,
467-
numba_cache_key=numba_cache_key,
445+
numba_cache_key=(lambda x: x, "ewma"),
468446
)
469447
elif engine in ("cython", None):
470448
if engine_kwargs is not None:

pandas/core/window/numba_.py

-80
Original file line numberDiff line numberDiff line change
@@ -248,83 +248,3 @@ def nan_agg_with_axis(table):
248248
return result
249249

250250
return nan_agg_with_axis
251-
252-
253-
def generate_ewma_numba_table_func(
254-
engine_kwargs: dict[str, bool] | None,
255-
com: float,
256-
adjust: bool,
257-
ignore_na: bool,
258-
deltas: np.ndarray,
259-
):
260-
"""
261-
Generate a numba jitted ewma function applied table wise specified
262-
by values from engine_kwargs.
263-
264-
Parameters
265-
----------
266-
engine_kwargs : dict
267-
dictionary of arguments to be passed into numba.jit
268-
com : float
269-
adjust : bool
270-
ignore_na : bool
271-
deltas : numpy.ndarray
272-
273-
Returns
274-
-------
275-
Numba function
276-
"""
277-
nopython, nogil, parallel = get_jit_arguments(engine_kwargs)
278-
279-
cache_key = (lambda x: x, "ewma_table")
280-
if cache_key in NUMBA_FUNC_CACHE:
281-
return NUMBA_FUNC_CACHE[cache_key]
282-
283-
numba = import_optional_dependency("numba")
284-
285-
@numba.jit(nopython=nopython, nogil=nogil, parallel=parallel)
286-
def ewma_table(
287-
values: np.ndarray,
288-
begin: np.ndarray,
289-
end: np.ndarray,
290-
minimum_periods: int,
291-
) -> np.ndarray:
292-
alpha = 1.0 / (1.0 + com)
293-
old_wt_factor = 1.0 - alpha
294-
new_wt = 1.0 if adjust else alpha
295-
old_wt = np.ones(values.shape[0])
296-
297-
result = np.empty(values.shape)
298-
weighted_avg = values[0].copy()
299-
nobs = (~np.isnan(weighted_avg)).astype(np.int64)
300-
result[0] = np.where(nobs >= minimum_periods, weighted_avg, np.nan)
301-
302-
for i in range(1, len(values)):
303-
cur = values[i]
304-
is_observations = ~np.isnan(cur)
305-
nobs += is_observations.astype(np.int64)
306-
for j in numba.prange(len(cur)):
307-
if not np.isnan(weighted_avg[j]):
308-
if is_observations[j] or not ignore_na:
309-
310-
# note that len(deltas) = len(vals) - 1 and deltas[i] is to be
311-
# used in conjunction with vals[i+1]
312-
old_wt[j] *= old_wt_factor ** deltas[j - 1]
313-
if is_observations[j]:
314-
# avoid numerical errors on constant series
315-
if weighted_avg[j] != cur[j]:
316-
weighted_avg[j] = (
317-
(old_wt[j] * weighted_avg[j]) + (new_wt * cur[j])
318-
) / (old_wt[j] + new_wt)
319-
if adjust:
320-
old_wt[j] += new_wt
321-
else:
322-
old_wt[j] = 1.0
323-
elif is_observations[j]:
324-
weighted_avg[j] = cur[j]
325-
326-
result[i] = np.where(nobs >= minimum_periods, weighted_avg, np.nan)
327-
328-
return result
329-
330-
return ewma_table

pandas/tests/window/test_numba.py

-13
Original file line numberDiff line numberDiff line change
@@ -304,16 +304,3 @@ def test_table_method_expanding_methods(
304304
engine_kwargs=engine_kwargs, engine="numba"
305305
)
306306
tm.assert_frame_equal(result, expected)
307-
308-
def test_table_method_ewm(self, axis, nogil, parallel, nopython):
309-
engine_kwargs = {"nogil": nogil, "parallel": parallel, "nopython": nopython}
310-
311-
df = DataFrame(np.eye(3))
312-
313-
result = df.ewm(com=1, method="table", axis=axis).mean(
314-
engine_kwargs=engine_kwargs, engine="numba"
315-
)
316-
expected = df.ewm(com=1, method="single", axis=axis).mean(
317-
engine_kwargs=engine_kwargs, engine="numba"
318-
)
319-
tm.assert_frame_equal(result, expected)

0 commit comments

Comments
 (0)