diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 2f8cb346935a9..17a5c292112b9 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -271,6 +271,7 @@ Other Deprecations - Deprecated dropping of nuisance columns in :class:`Rolling`, :class:`Expanding`, and :class:`EWM` aggregations (:issue:`42738`) - Deprecated :meth:`Index.reindex` with a non-unique index (:issue:`42568`) - Deprecated :meth:`.Styler.render` in favour of :meth:`.Styler.to_html` (:issue:`42140`) +- Deprecated passing in a string column label into ``times`` in :meth:`DataFrame.ewm` (:issue:`43265`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/window/ewm.py b/pandas/core/window/ewm.py index f0bba8ae9727f..7b58af87fb1d8 100644 --- a/pandas/core/window/ewm.py +++ b/pandas/core/window/ewm.py @@ -21,6 +21,7 @@ from pandas.compat.numpy import function as nv from pandas.util._decorators import doc +from pandas.util._exceptions import find_stack_level from pandas.core.dtypes.common import is_datetime64_ns_dtype from pandas.core.dtypes.missing import isna @@ -315,6 +316,15 @@ def __init__( if not self.adjust: raise NotImplementedError("times is not supported with adjust=False.") if isinstance(self.times, str): + warnings.warn( + ( + "Specifying times as a string column label is deprecated " + "and will be removed in a future version. Pass the column " + "into times instead." + ), + FutureWarning, + stacklevel=find_stack_level(), + ) self.times = self._selected_obj[self.times] if not is_datetime64_ns_dtype(self.times): raise ValueError("times must be datetime64[ns] dtype.") diff --git a/pandas/tests/window/test_ewm.py b/pandas/tests/window/test_ewm.py index e3ea53121defa..5579444f99bbb 100644 --- a/pandas/tests/window/test_ewm.py +++ b/pandas/tests/window/test_ewm.py @@ -107,7 +107,6 @@ def test_ewma_halflife_without_times(halflife_with_times): np.arange(10).astype("datetime64[D]").astype("datetime64[ns]"), date_range("2000", freq="D", periods=10), date_range("2000", freq="D", periods=10).tz_localize("UTC"), - "time_col", ], ) @pytest.mark.parametrize("min_periods", [0, 2]) @@ -231,3 +230,14 @@ def test_float_dtype_ewma(func, expected, float_numpy_dtype): result = getattr(e, func)() tm.assert_frame_equal(result, expected) + + +def test_times_string_col_deprecated(): + # GH 43265 + data = np.arange(10.0) + data[::2] = np.nan + df = DataFrame({"A": data, "time_col": date_range("2000", freq="D", periods=10)}) + with tm.assert_produces_warning(FutureWarning, match="Specifying times"): + result = df.ewm(halflife="1 day", min_periods=0, times="time_col").mean() + expected = df.ewm(halflife=1.0, min_periods=0).mean() + tm.assert_frame_equal(result, expected)