From eca64294d0ef6a15a8056d95cc284f40bc4c0bd5 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Fri, 27 Aug 2021 13:58:52 -0700 Subject: [PATCH 1/4] DEPR: Passing in a string column label for DataFrame.ewm(times=...) --- doc/source/whatsnew/v1.4.0.rst | 1 + pandas/core/window/ewm.py | 9 +++++++++ pandas/tests/window/test_ewm.py | 12 +++++++++++- 3 files changed, 21 insertions(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index fc488504f1fdf..17a85357aa782 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -228,6 +228,7 @@ 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:``) .. --------------------------------------------------------------------------- diff --git a/pandas/core/window/ewm.py b/pandas/core/window/ewm.py index f0bba8ae9727f..0ccf7bc22bd52 100644 --- a/pandas/core/window/ewm.py +++ b/pandas/core/window/ewm.py @@ -315,6 +315,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=3, + ) 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..96fc4328e3909 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 + 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) From db0ca21b1f295b4b5709ce50138e811d6e3e9f19 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Fri, 27 Aug 2021 16:44:50 -0700 Subject: [PATCH 2/4] add PR number --- doc/source/whatsnew/v1.4.0.rst | 2 +- pandas/tests/window/test_ewm.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 6ed6c75da1f49..27ca050697344 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -231,7 +231,7 @@ 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:``) +- Deprecated passing in a string column label into ``times`` in :meth:`DataFrame.ewm.mean` (:issue:`43265`) .. --------------------------------------------------------------------------- diff --git a/pandas/tests/window/test_ewm.py b/pandas/tests/window/test_ewm.py index 96fc4328e3909..5579444f99bbb 100644 --- a/pandas/tests/window/test_ewm.py +++ b/pandas/tests/window/test_ewm.py @@ -233,7 +233,7 @@ def test_float_dtype_ewma(func, expected, float_numpy_dtype): def test_times_string_col_deprecated(): - # GH + # GH 43265 data = np.arange(10.0) data[::2] = np.nan df = DataFrame({"A": data, "time_col": date_range("2000", freq="D", periods=10)}) From 95a99ae5d7eff1adae5f4ce79c73a13ee8196cfe Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Fri, 27 Aug 2021 16:45:39 -0700 Subject: [PATCH 3/4] Technically not mean --- doc/source/whatsnew/v1.4.0.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index 27ca050697344..8cac8911e70d2 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -231,7 +231,7 @@ 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.mean` (:issue:`43265`) +- Deprecated passing in a string column label into ``times`` in :meth:`DataFrame.ewm` (:issue:`43265`) .. --------------------------------------------------------------------------- From 0b5a847e0980d3ffd736e06b007ce30e8dc2e2c9 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke Date: Sun, 29 Aug 2021 11:57:45 -0700 Subject: [PATCH 4/4] use find_stack_level --- pandas/core/window/ewm.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pandas/core/window/ewm.py b/pandas/core/window/ewm.py index 0ccf7bc22bd52..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 @@ -322,7 +323,7 @@ def __init__( "into times instead." ), FutureWarning, - stacklevel=3, + stacklevel=find_stack_level(), ) self.times = self._selected_obj[self.times] if not is_datetime64_ns_dtype(self.times):