From 1e91e06e9d670aba15f7f92085e6cde5e24199e9 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Tue, 27 Sep 2022 17:13:12 -0700 Subject: [PATCH 1/3] DEPR: Enforce DTI.to_series(keep_tz) removal --- pandas/core/indexes/datetimes.py | 47 +------------------ pandas/tests/frame/indexing/test_setitem.py | 6 +-- .../datetimes/methods/test_to_series.py | 26 +--------- 3 files changed, 5 insertions(+), 74 deletions(-) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index bccbc2f861bdd..e6effa3b13a17 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -519,7 +519,7 @@ def _get_time_micros(self) -> npt.NDArray[np.int64]: micros[self._isnan] = -1 return micros - def to_series(self, keep_tz=lib.no_default, index=None, name=None): + def to_series(self, index=None, name=None): """ Create a Series with both index and values equal to the index keys. @@ -527,27 +527,6 @@ def to_series(self, keep_tz=lib.no_default, index=None, name=None): Parameters ---------- - keep_tz : optional, defaults True - Return the data keeping the timezone. - - If keep_tz is True: - - If the timezone is not set, the resulting - Series will have a datetime64[ns] dtype. - - Otherwise the Series will have an datetime64[ns, tz] dtype; the - tz will be preserved. - - If keep_tz is False: - - Series will have a datetime64[ns] dtype. TZ aware - objects will have the tz removed. - - .. versionchanged:: 1.0.0 - The default value is now True. In a future version, - this keyword will be removed entirely. Stop passing the - argument to obtain the future behavior and silence the warning. - index : Index, optional Index of resulting Series. If None, defaults to original index. name : str, optional @@ -565,29 +544,7 @@ def to_series(self, keep_tz=lib.no_default, index=None, name=None): if name is None: name = self.name - if keep_tz is not lib.no_default: - if keep_tz: - warnings.warn( - "The 'keep_tz' keyword in DatetimeIndex.to_series " - "is deprecated and will be removed in a future version. " - "You can stop passing 'keep_tz' to silence this warning.", - FutureWarning, - stacklevel=find_stack_level(inspect.currentframe()), - ) - else: - warnings.warn( - "Specifying 'keep_tz=False' is deprecated and this " - "option will be removed in a future release. If " - "you want to remove the timezone information, you " - "can do 'idx.tz_convert(None)' before calling " - "'to_series'.", - FutureWarning, - stacklevel=find_stack_level(inspect.currentframe()), - ) - else: - keep_tz = True - - if keep_tz and self.tz is not None: + if self.tz is not None: # preserve the tz & copy values = self.copy(deep=True) else: diff --git a/pandas/tests/frame/indexing/test_setitem.py b/pandas/tests/frame/indexing/test_setitem.py index cf0ff4e3603f3..f6a6df163c0d2 100644 --- a/pandas/tests/frame/indexing/test_setitem.py +++ b/pandas/tests/frame/indexing/test_setitem.py @@ -768,11 +768,7 @@ def test_setitem_dt64series(self, idx, expected): # convert to utc df = DataFrame(np.random.randn(2, 1), columns=["A"]) df["B"] = idx - - with tm.assert_produces_warning(FutureWarning) as m: - df["B"] = idx.to_series(keep_tz=False, index=[0, 1]) - msg = "do 'idx.tz_convert(None)' before calling" - assert msg in str(m[0].message) + df["B"] = idx.to_series(index=[0, 1]).dt.tz_convert(None) result = df["B"] comp = Series(idx.tz_convert("UTC").tz_localize(None), name="B") diff --git a/pandas/tests/indexes/datetimes/methods/test_to_series.py b/pandas/tests/indexes/datetimes/methods/test_to_series.py index 5a216d3c89899..0c397c8ab2cd3 100644 --- a/pandas/tests/indexes/datetimes/methods/test_to_series.py +++ b/pandas/tests/indexes/datetimes/methods/test_to_series.py @@ -1,5 +1,4 @@ import numpy as np -import pytest from pandas import ( DatetimeIndex, @@ -9,32 +8,11 @@ class TestToSeries: - @pytest.fixture - def idx_expected(self): + def test_to_series(self): naive = DatetimeIndex(["2013-1-1 13:00", "2013-1-2 14:00"], name="B") idx = naive.tz_localize("US/Pacific") expected = Series(np.array(idx.tolist(), dtype="object"), name="B") - + result = idx.to_series(index=[0, 1]) assert expected.dtype == idx.dtype - return idx, expected - - def test_to_series_keep_tz_deprecated_true(self, idx_expected): - # convert to series while keeping the timezone - idx, expected = idx_expected - - msg = "stop passing 'keep_tz'" - with tm.assert_produces_warning(FutureWarning) as m: - result = idx.to_series(keep_tz=True, index=[0, 1]) - assert msg in str(m[0].message) - tm.assert_series_equal(result, expected) - - def test_to_series_keep_tz_deprecated_false(self, idx_expected): - idx, expected = idx_expected - - with tm.assert_produces_warning(FutureWarning) as m: - result = idx.to_series(keep_tz=False, index=[0, 1]) - tm.assert_series_equal(result, expected.dt.tz_convert(None)) - msg = "do 'idx.tz_convert(None)' before calling" - assert msg in str(m[0].message) From 72b3baf0a9b14f8e0bac7ec6ece6cb3af1064c37 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 19 Oct 2022 10:49:32 -0700 Subject: [PATCH 2/3] Simplify copy --- pandas/core/indexes/datetimes.py | 8 +------- 1 file changed, 1 insertion(+), 7 deletions(-) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 5be6482c84396..b08a0d3a60526 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -543,13 +543,7 @@ def to_series(self, index=None, name=None): if name is None: name = self.name - if self.tz is not None: - # preserve the tz & copy - values = self.copy(deep=True) - else: - # error: Incompatible types in assignment (expression has type - # "Union[ExtensionArray, ndarray]", variable has type "DatetimeIndex") - values = self._values.view("M8[ns]").copy() # type: ignore[assignment] + values = self._values.copy() return Series(values, index=index, name=name) From 547221036a950ad9e5f238e1352bcd381057aa09 Mon Sep 17 00:00:00 2001 From: Matthew Roeschke <10647082+mroeschke@users.noreply.github.com> Date: Wed, 19 Oct 2022 10:51:20 -0700 Subject: [PATCH 3/3] Add whatsnew --- doc/source/whatsnew/v2.0.0.rst | 1 + 1 file changed, 1 insertion(+) diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 596c16a5b3621..bb87079d70697 100644 --- a/doc/source/whatsnew/v2.0.0.rst +++ b/doc/source/whatsnew/v2.0.0.rst @@ -144,6 +144,7 @@ Deprecations Removal of prior version deprecations/changes ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +- Removed ``keep_tz`` argument in :meth:`DatetimeIndex.to_series` (:issue:`29731`) - Disallow passing non-round floats to :class:`Timestamp` with ``unit="M"`` or ``unit="Y"`` (:issue:`47266`) - Remove :meth:`DataFrameGroupBy.pad` and :meth:`DataFrameGroupBy.backfill` (:issue:`45076`)