From cb73b15a43ddc2054b80876b93f492556aae897b Mon Sep 17 00:00:00 2001 From: tdang2k Date: Sat, 17 Feb 2024 12:11:47 +0100 Subject: [PATCH 1/6] Raise warning on deprecated feature of to_pytimedelta --- doc/source/whatsnew/v3.0.0.rst | 2 +- pandas/conftest.py | 4 ++++ pandas/core/indexes/accessors.py | 18 ++++++++++++++++++ pandas/tests/extension/test_arrow.py | 8 ++++++-- .../tests/series/accessors/test_dt_accessor.py | 4 +++- 5 files changed, 32 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 483cf659080ea..a3b03fd417886 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -102,7 +102,7 @@ Deprecations ~~~~~~~~~~~~ - Deprecated :meth:`Timestamp.utcfromtimestamp`, use ``Timestamp.fromtimestamp(ts, "UTC")`` instead (:issue:`56680`) - Deprecated :meth:`Timestamp.utcnow`, use ``Timestamp.now("UTC")`` instead (:issue:`56680`) -- +- Deprecated behavior of :meth:`Series.dt.to_pytimedelta`, in a future version this will return a :class:`Series` containing python ``timedelta`` objects instead of an ``ndarray`` of timedelta; this matches the behavior of other :meth:`Series.dt` properties (:issue:`57463`) .. --------------------------------------------------------------------------- .. _whatsnew_300.prior_deprecations: diff --git a/pandas/conftest.py b/pandas/conftest.py index 254d605e13460..236f14bcf47e2 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -162,6 +162,10 @@ def pytest_collection_modifyitems(items, config) -> None: "to_pydatetime", "The behavior of DatetimeProperties.to_pydatetime is deprecated", ), + ( + "to_pytimedelta", + "The behavior of DatetimeProperties.to_pytimedelta is deprecated", + ), ( "pandas.core.generic.NDFrame.first", "first is deprecated and will be removed in a future version. " diff --git a/pandas/core/indexes/accessors.py b/pandas/core/indexes/accessors.py index 8a742a0a9d57d..eb700cbf706fd 100644 --- a/pandas/core/indexes/accessors.py +++ b/pandas/core/indexes/accessors.py @@ -211,6 +211,15 @@ def _delegate_method(self, name: str, *args, **kwargs): return result def to_pytimedelta(self): + # GH#57463 + warnings.warn( + f"The behavior of {type(self).__name__}.to_pytimedelta is deprecated, " + "in a future version this will return a Series containing python " + "timedelta objects instead of an ndarray. To retain the old behavior, " + "call `np.array` on the result", + FutureWarning, + stacklevel=find_stack_level(), + ) return cast(ArrowExtensionArray, self._parent.array)._dt_to_pytimedelta() def to_pydatetime(self): @@ -481,6 +490,15 @@ def to_pytimedelta(self) -> np.ndarray: datetime.timedelta(days=2), datetime.timedelta(days=3), datetime.timedelta(days=4)], dtype=object) """ + # GH#57463 + warnings.warn( + f"The behavior of {type(self).__name__}.to_pytimedelta is deprecated, " + "in a future version this will return a Series containing python " + "timedelta objects instead of an ndarray. To retain the old behavior, " + "call `np.array` on the result", + FutureWarning, + stacklevel=find_stack_level(), + ) return self._get_values().to_pytimedelta() @property diff --git a/pandas/tests/extension/test_arrow.py b/pandas/tests/extension/test_arrow.py index fe6e484fbd6e7..90e4b84706d47 100644 --- a/pandas/tests/extension/test_arrow.py +++ b/pandas/tests/extension/test_arrow.py @@ -2867,12 +2867,16 @@ def test_dt_to_pytimedelta(): data = [timedelta(1, 2, 3), timedelta(1, 2, 4)] ser = pd.Series(data, dtype=ArrowDtype(pa.duration("ns"))) - result = ser.dt.to_pytimedelta() + msg = "The behavior of ArrowTemporalProperties.to_pytimedelta is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + result = ser.dt.to_pytimedelta() expected = np.array(data, dtype=object) tm.assert_numpy_array_equal(result, expected) assert all(type(res) is timedelta for res in result) - expected = ser.astype("timedelta64[ns]").dt.to_pytimedelta() + msg = "The behavior of DatetimeProperties.to_pytimedelta is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + expected = ser.astype("timedelta64[ns]").dt.to_pytimedelta() tm.assert_numpy_array_equal(result, expected) diff --git a/pandas/tests/series/accessors/test_dt_accessor.py b/pandas/tests/series/accessors/test_dt_accessor.py index 17492f17132fd..868820490ad3d 100644 --- a/pandas/tests/series/accessors/test_dt_accessor.py +++ b/pandas/tests/series/accessors/test_dt_accessor.py @@ -196,7 +196,9 @@ def test_dt_namespace_accessor_timedelta(self): assert isinstance(result, DataFrame) tm.assert_index_equal(result.index, ser.index) - result = ser.dt.to_pytimedelta() + msg = "The behavior of DatetimeProperties.to_pytimedelta is deprecated" + with tm.assert_produces_warning(FutureWarning, match=msg): + result = ser.dt.to_pytimedelta() assert isinstance(result, np.ndarray) assert result.dtype == object From b7bb0db25a5b1b71f554dc24ff6065aafb8af442 Mon Sep 17 00:00:00 2001 From: tdang2k Date: Sat, 17 Feb 2024 18:59:12 +0100 Subject: [PATCH 2/6] Append FutureWarning when calling to_pytimedelta --- doc/source/whatsnew/v3.0.0.rst | 1 - pandas/tests/series/accessors/test_cat_accessor.py | 2 +- pandas/tests/series/accessors/test_dt_accessor.py | 2 +- 3 files changed, 2 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index a3b03fd417886..30eb02f3f0d25 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -103,7 +103,6 @@ Deprecations - Deprecated :meth:`Timestamp.utcfromtimestamp`, use ``Timestamp.fromtimestamp(ts, "UTC")`` instead (:issue:`56680`) - Deprecated :meth:`Timestamp.utcnow`, use ``Timestamp.now("UTC")`` instead (:issue:`56680`) - Deprecated behavior of :meth:`Series.dt.to_pytimedelta`, in a future version this will return a :class:`Series` containing python ``timedelta`` objects instead of an ``ndarray`` of timedelta; this matches the behavior of other :meth:`Series.dt` properties (:issue:`57463`) - .. --------------------------------------------------------------------------- .. _whatsnew_300.prior_deprecations: diff --git a/pandas/tests/series/accessors/test_cat_accessor.py b/pandas/tests/series/accessors/test_cat_accessor.py index 2d50b0f36904a..b9b39c255a051 100644 --- a/pandas/tests/series/accessors/test_cat_accessor.py +++ b/pandas/tests/series/accessors/test_cat_accessor.py @@ -200,7 +200,7 @@ def test_dt_accessor_api_for_categorical(self, idx): if func == "to_period" and getattr(idx, "tz", None) is not None: # dropping TZ warn_cls.append(UserWarning) - if func == "to_pydatetime": + if func == "to_pydatetime" or func == "to_pytimedelta": # deprecated to return Index[object] warn_cls.append(FutureWarning) if warn_cls: diff --git a/pandas/tests/series/accessors/test_dt_accessor.py b/pandas/tests/series/accessors/test_dt_accessor.py index 868820490ad3d..d431b4fbc9809 100644 --- a/pandas/tests/series/accessors/test_dt_accessor.py +++ b/pandas/tests/series/accessors/test_dt_accessor.py @@ -196,7 +196,7 @@ def test_dt_namespace_accessor_timedelta(self): assert isinstance(result, DataFrame) tm.assert_index_equal(result.index, ser.index) - msg = "The behavior of DatetimeProperties.to_pytimedelta is deprecated" + msg = "The behavior of TimedeltaProperties.to_pytimedelta is deprecated" with tm.assert_produces_warning(FutureWarning, match=msg): result = ser.dt.to_pytimedelta() assert isinstance(result, np.ndarray) From 012f0e8d27a23ef8cbffb422dd990ff4492d3c7b Mon Sep 17 00:00:00 2001 From: tdang2k Date: Sat, 17 Feb 2024 19:47:32 +0100 Subject: [PATCH 3/6] Fix class name and resolve conflict --- doc/source/whatsnew/v3.0.0.rst | 5 ++++- pandas/core/indexes/accessors.py | 4 ++-- pandas/tests/extension/test_arrow.py | 2 +- 3 files changed, 7 insertions(+), 4 deletions(-) diff --git a/doc/source/whatsnew/v3.0.0.rst b/doc/source/whatsnew/v3.0.0.rst index 30eb02f3f0d25..4b6860db3d1d5 100644 --- a/doc/source/whatsnew/v3.0.0.rst +++ b/doc/source/whatsnew/v3.0.0.rst @@ -102,7 +102,10 @@ Deprecations ~~~~~~~~~~~~ - Deprecated :meth:`Timestamp.utcfromtimestamp`, use ``Timestamp.fromtimestamp(ts, "UTC")`` instead (:issue:`56680`) - Deprecated :meth:`Timestamp.utcnow`, use ``Timestamp.now("UTC")`` instead (:issue:`56680`) -- Deprecated behavior of :meth:`Series.dt.to_pytimedelta`, in a future version this will return a :class:`Series` containing python ``timedelta`` objects instead of an ``ndarray`` of timedelta; this matches the behavior of other :meth:`Series.dt` properties (:issue:`57463`) +- Deprecated allowing non-keyword arguments in :meth:`Series.to_string` except ``buf``. (:issue:`57280`) +- Deprecated behavior of :meth:`Series.dt.to_pytimedelta`, in a future version this will return a :class:`Series` containing python ``datetime.timedelta`` objects instead of an ``ndarray`` of timedelta; this matches the behavior of other :meth:`Series.dt` properties (:issue:`57463`) +- + .. --------------------------------------------------------------------------- .. _whatsnew_300.prior_deprecations: diff --git a/pandas/core/indexes/accessors.py b/pandas/core/indexes/accessors.py index eb700cbf706fd..ca9f6694bc175 100644 --- a/pandas/core/indexes/accessors.py +++ b/pandas/core/indexes/accessors.py @@ -215,7 +215,7 @@ def to_pytimedelta(self): warnings.warn( f"The behavior of {type(self).__name__}.to_pytimedelta is deprecated, " "in a future version this will return a Series containing python " - "timedelta objects instead of an ndarray. To retain the old behavior, " + "datetime.timedelta objects instead of an ndarray. To retain the old behavior, " "call `np.array` on the result", FutureWarning, stacklevel=find_stack_level(), @@ -494,7 +494,7 @@ def to_pytimedelta(self) -> np.ndarray: warnings.warn( f"The behavior of {type(self).__name__}.to_pytimedelta is deprecated, " "in a future version this will return a Series containing python " - "timedelta objects instead of an ndarray. To retain the old behavior, " + "datetime.timedelta objects instead of an ndarray. To retain the old behavior, " "call `np.array` on the result", FutureWarning, stacklevel=find_stack_level(), diff --git a/pandas/tests/extension/test_arrow.py b/pandas/tests/extension/test_arrow.py index 90e4b84706d47..0e047a235289c 100644 --- a/pandas/tests/extension/test_arrow.py +++ b/pandas/tests/extension/test_arrow.py @@ -2874,7 +2874,7 @@ def test_dt_to_pytimedelta(): tm.assert_numpy_array_equal(result, expected) assert all(type(res) is timedelta for res in result) - msg = "The behavior of DatetimeProperties.to_pytimedelta is deprecated" + msg = "The behavior of TimedeltaProperties.to_pytimedelta is deprecated" with tm.assert_produces_warning(FutureWarning, match=msg): expected = ser.astype("timedelta64[ns]").dt.to_pytimedelta() tm.assert_numpy_array_equal(result, expected) From e298b1804d7152ca2d1d94fb39492b3b0537e06f Mon Sep 17 00:00:00 2001 From: tdang2k Date: Sat, 17 Feb 2024 21:08:32 +0100 Subject: [PATCH 4/6] Fix line too long and merge comparisons error --- pandas/core/indexes/accessors.py | 8 ++++---- pandas/tests/series/accessors/test_cat_accessor.py | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pandas/core/indexes/accessors.py b/pandas/core/indexes/accessors.py index ca9f6694bc175..5973f0315842b 100644 --- a/pandas/core/indexes/accessors.py +++ b/pandas/core/indexes/accessors.py @@ -215,8 +215,8 @@ def to_pytimedelta(self): warnings.warn( f"The behavior of {type(self).__name__}.to_pytimedelta is deprecated, " "in a future version this will return a Series containing python " - "datetime.timedelta objects instead of an ndarray. To retain the old behavior, " - "call `np.array` on the result", + "datetime.timedelta objects instead of an ndarray. To retain " + "the old behavior, call `np.array` on the result", FutureWarning, stacklevel=find_stack_level(), ) @@ -494,8 +494,8 @@ def to_pytimedelta(self) -> np.ndarray: warnings.warn( f"The behavior of {type(self).__name__}.to_pytimedelta is deprecated, " "in a future version this will return a Series containing python " - "datetime.timedelta objects instead of an ndarray. To retain the old behavior, " - "call `np.array` on the result", + "datetime.timedelta objects instead of an ndarray. To retain " + "the old behavior, call `np.array` on the result", FutureWarning, stacklevel=find_stack_level(), ) diff --git a/pandas/tests/series/accessors/test_cat_accessor.py b/pandas/tests/series/accessors/test_cat_accessor.py index b9b39c255a051..e29e140b8c054 100644 --- a/pandas/tests/series/accessors/test_cat_accessor.py +++ b/pandas/tests/series/accessors/test_cat_accessor.py @@ -200,7 +200,7 @@ def test_dt_accessor_api_for_categorical(self, idx): if func == "to_period" and getattr(idx, "tz", None) is not None: # dropping TZ warn_cls.append(UserWarning) - if func == "to_pydatetime" or func == "to_pytimedelta": + if func in {"to_pydatetime", "to_pytimedelta"}: # deprecated to return Index[object] warn_cls.append(FutureWarning) if warn_cls: From 51c3b054af5cbbd587fde77645b109d30df86888 Mon Sep 17 00:00:00 2001 From: tdang2k Date: Tue, 27 Feb 2024 18:16:22 +0100 Subject: [PATCH 5/6] Fix doctest warning --- pandas/conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/conftest.py b/pandas/conftest.py index 236f14bcf47e2..0e3bab3cbf0a4 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -164,7 +164,7 @@ def pytest_collection_modifyitems(items, config) -> None: ), ( "to_pytimedelta", - "The behavior of DatetimeProperties.to_pytimedelta is deprecated", + "The behavior of TimedeltaProperties.to_pytimedelta is deprecated", ), ( "pandas.core.generic.NDFrame.first", From 028a963942547303e6f1c005e97e8985f5a06625 Mon Sep 17 00:00:00 2001 From: tdang2k Date: Tue, 27 Feb 2024 18:27:27 +0100 Subject: [PATCH 6/6] Remove changes related to pydatetime --- pandas/conftest.py | 4 ---- pandas/tests/series/accessors/test_cat_accessor.py | 2 +- 2 files changed, 1 insertion(+), 5 deletions(-) diff --git a/pandas/conftest.py b/pandas/conftest.py index 3039624d1635a..0a84b060581ad 100644 --- a/pandas/conftest.py +++ b/pandas/conftest.py @@ -159,10 +159,6 @@ def pytest_collection_modifyitems(items, config) -> None: ("SeriesGroupBy.idxmax", "The behavior of Series.idxmax"), # Docstring divides by zero to show behavior difference ("missing.mask_zero_div_zero", "divide by zero encountered"), - ( - "to_pydatetime", - "The behavior of DatetimeProperties.to_pydatetime is deprecated", - ), ( "to_pytimedelta", "The behavior of TimedeltaProperties.to_pytimedelta is deprecated", diff --git a/pandas/tests/series/accessors/test_cat_accessor.py b/pandas/tests/series/accessors/test_cat_accessor.py index e29e140b8c054..b69021ffbacca 100644 --- a/pandas/tests/series/accessors/test_cat_accessor.py +++ b/pandas/tests/series/accessors/test_cat_accessor.py @@ -200,7 +200,7 @@ def test_dt_accessor_api_for_categorical(self, idx): if func == "to_period" and getattr(idx, "tz", None) is not None: # dropping TZ warn_cls.append(UserWarning) - if func in {"to_pydatetime", "to_pytimedelta"}: + if func == "to_pytimedelta": # deprecated to return Index[object] warn_cls.append(FutureWarning) if warn_cls: