Skip to content

Commit 711fd8f

Browse files
mroeschkephofl
authored andcommitted
DEPR: Enforce ewm(times=str) deprecation (#48842)
* DEPR: Enforce ewm(times=str) deprecation * Fix typing * Fix test * convert test to raises * Add whatsnew * Fix typing * fix typing
1 parent 2efa8e5 commit 711fd8f

File tree

5 files changed

+28
-45
lines changed

5 files changed

+28
-45
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ Removal of prior version deprecations/changes
180180
- Remove :meth:`DataFrameGroupBy.pad` and :meth:`DataFrameGroupBy.backfill` (:issue:`45076`)
181181
- Remove ``numpy`` argument from :func:`read_json` (:issue:`30636`)
182182
- Removed the ``center`` keyword in :meth:`DataFrame.expanding` (:issue:`20647`)
183+
- Enforced disallowing a string column label into ``times`` in :meth:`DataFrame.ewm` (:issue:`43265`)
183184
- Enforced :meth:`Rolling.count` with ``min_periods=None`` to default to the size of the window (:issue:`31302`)
184185
-
185186

pandas/core/generic.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -12089,7 +12089,7 @@ def ewm(
1208912089
adjust: bool_t = True,
1209012090
ignore_na: bool_t = False,
1209112091
axis: Axis = 0,
12092-
times: str | np.ndarray | DataFrame | Series | None = None,
12092+
times: np.ndarray | DataFrame | Series | None = None,
1209312093
method: str = "single",
1209412094
) -> ExponentialMovingWindow:
1209512095
axis = self._get_axis_number(axis)

pandas/core/window/ewm.py

+8-32
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
import datetime
44
from functools import partial
55
from textwrap import dedent
6-
from typing import (
7-
TYPE_CHECKING,
8-
cast,
9-
)
6+
from typing import TYPE_CHECKING
107
import warnings
118

129
import numpy as np
@@ -106,7 +103,7 @@ def get_center_of_mass(
106103

107104

108105
def _calculate_deltas(
109-
times: str | np.ndarray | NDFrame | None,
106+
times: np.ndarray | NDFrame,
110107
halflife: float | TimedeltaConvertibleTypes | None,
111108
) -> np.ndarray:
112109
"""
@@ -115,7 +112,7 @@ def _calculate_deltas(
115112
116113
Parameters
117114
----------
118-
times : str, np.ndarray, Series, default None
115+
times : np.ndarray, Series
119116
Times corresponding to the observations. Must be monotonically increasing
120117
and ``datetime64[ns]`` dtype.
121118
halflife : float, str, timedelta, optional
@@ -126,13 +123,7 @@ def _calculate_deltas(
126123
np.ndarray
127124
Diff of the times divided by the half-life
128125
"""
129-
# error: Item "str" of "Union[str, ndarray, NDFrameT, None]" has no
130-
# attribute "view"
131-
# error: Item "None" of "Union[str, ndarray, NDFrameT, None]" has no
132-
# attribute "view"
133-
_times = np.asarray(
134-
times.view(np.int64), dtype=np.float64 # type: ignore[union-attr]
135-
)
126+
_times = np.asarray(times.view(np.int64), dtype=np.float64)
136127
# TODO: generalize to non-nano?
137128
_halflife = float(Timedelta(halflife)._as_unit("ns").value)
138129
return np.diff(_times) / _halflife
@@ -221,7 +212,7 @@ class ExponentialMovingWindow(BaseWindow):
221212
222213
For `Series` this parameter is unused and defaults to 0.
223214
224-
times : str, np.ndarray, Series, default None
215+
times : np.ndarray, Series, default None
225216
226217
.. versionadded:: 1.1.0
227218
@@ -232,9 +223,6 @@ class ExponentialMovingWindow(BaseWindow):
232223
233224
If 1-D array like, a sequence with the same shape as the observations.
234225
235-
.. deprecated:: 1.4.0
236-
If str, the name of the column in the DataFrame representing the times.
237-
238226
method : str {'single', 'table'}, default 'single'
239227
.. versionadded:: 1.4.0
240228
@@ -359,7 +347,7 @@ def __init__(
359347
adjust: bool = True,
360348
ignore_na: bool = False,
361349
axis: Axis = 0,
362-
times: str | np.ndarray | NDFrame | None = None,
350+
times: np.ndarray | NDFrame | None = None,
363351
method: str = "single",
364352
*,
365353
selection=None,
@@ -384,18 +372,6 @@ def __init__(
384372
if self.times is not None:
385373
if not self.adjust:
386374
raise NotImplementedError("times is not supported with adjust=False.")
387-
if isinstance(self.times, str):
388-
warnings.warn(
389-
(
390-
"Specifying times as a string column label is deprecated "
391-
"and will be removed in a future version. Pass the column "
392-
"into times instead."
393-
),
394-
FutureWarning,
395-
stacklevel=find_stack_level(),
396-
)
397-
# self.times cannot be str anymore
398-
self.times = cast("Series", self._selected_obj[self.times])
399375
if not is_datetime64_ns_dtype(self.times):
400376
raise ValueError("times must be datetime64[ns] dtype.")
401377
if len(self.times) != len(obj):
@@ -897,7 +873,7 @@ def __init__(self, obj, *args, _grouper=None, **kwargs) -> None:
897873
# sort the times and recalculate the deltas according to the groups
898874
groupby_order = np.concatenate(list(self._grouper.indices.values()))
899875
self._deltas = _calculate_deltas(
900-
self.times.take(groupby_order), # type: ignore[union-attr]
876+
self.times.take(groupby_order),
901877
self.halflife,
902878
)
903879

@@ -928,7 +904,7 @@ def __init__(
928904
adjust: bool = True,
929905
ignore_na: bool = False,
930906
axis: Axis = 0,
931-
times: str | np.ndarray | NDFrame | None = None,
907+
times: np.ndarray | NDFrame | None = None,
932908
engine: str = "numba",
933909
engine_kwargs: dict[str, bool] | None = None,
934910
*,

pandas/tests/window/test_ewm.py

+6-8
Original file line numberDiff line numberDiff line change
@@ -236,15 +236,13 @@ def test_float_dtype_ewma(func, expected, float_numpy_dtype):
236236
tm.assert_frame_equal(result, expected)
237237

238238

239-
def test_times_string_col_deprecated():
239+
def test_times_string_col_raises():
240240
# GH 43265
241-
data = np.arange(10.0)
242-
data[::2] = np.nan
243-
df = DataFrame({"A": data, "time_col": date_range("2000", freq="D", periods=10)})
244-
with tm.assert_produces_warning(FutureWarning, match="Specifying times"):
245-
result = df.ewm(halflife="1 day", min_periods=0, times="time_col").mean()
246-
expected = df.ewm(halflife=1.0, min_periods=0).mean()
247-
tm.assert_frame_equal(result, expected)
241+
df = DataFrame(
242+
{"A": np.arange(10.0), "time_col": date_range("2000", freq="D", periods=10)}
243+
)
244+
with pytest.raises(ValueError, match="times must be datetime64"):
245+
df.ewm(halflife="1 day", min_periods=0, times="time_col")
248246

249247

250248
def test_ewm_sum_adjust_false_notimplemented():

pandas/tests/window/test_groupby.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -1162,7 +1162,11 @@ def test_times(self, times_frame):
11621162
halflife = "23 days"
11631163
with tm.assert_produces_warning(FutureWarning, match="nuisance"):
11641164
# GH#42738
1165-
result = times_frame.groupby("A").ewm(halflife=halflife, times="C").mean()
1165+
result = (
1166+
times_frame.groupby("A")
1167+
.ewm(halflife=halflife, times=times_frame["C"])
1168+
.mean()
1169+
)
11661170
expected = DataFrame(
11671171
{
11681172
"B": [
@@ -1201,9 +1205,13 @@ def test_times_vs_apply(self, times_frame):
12011205
halflife = "23 days"
12021206
with tm.assert_produces_warning(FutureWarning, match="nuisance"):
12031207
# GH#42738
1204-
result = times_frame.groupby("A").ewm(halflife=halflife, times="C").mean()
1208+
result = (
1209+
times_frame.groupby("A")
1210+
.ewm(halflife=halflife, times=times_frame["C"])
1211+
.mean()
1212+
)
12051213
expected = times_frame.groupby("A", group_keys=True).apply(
1206-
lambda x: x.ewm(halflife=halflife, times="C").mean()
1214+
lambda x: x.ewm(halflife=halflife, times=x["C"]).mean()
12071215
)
12081216
tm.assert_frame_equal(result, expected)
12091217

@@ -1213,7 +1221,7 @@ def test_times_array(self, times_frame):
12131221
gb = times_frame.groupby("A")
12141222
with tm.assert_produces_warning(FutureWarning, match="nuisance"):
12151223
# GH#42738
1216-
result = gb.ewm(halflife=halflife, times="C").mean()
1224+
result = gb.ewm(halflife=halflife, times=times_frame["C"]).mean()
12171225
expected = gb.ewm(halflife=halflife, times=times_frame["C"].values).mean()
12181226
tm.assert_frame_equal(result, expected)
12191227

0 commit comments

Comments
 (0)