diff --git a/doc/source/whatsnew/v2.0.0.rst b/doc/source/whatsnew/v2.0.0.rst index 30acc117c237a..f295218dc4c76 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`) - Remove arguments ``names`` and ``dtype`` from :meth:`Index.copy` and ``levels`` and ``codes`` from :meth:`MultiIndex.copy` (:issue:`35853`, :issue:`36685`) - Disallow passing non-round floats to :class:`Timestamp` with ``unit="M"`` or ``unit="Y"`` (:issue:`47266`) - Removed :func:`is_extension_type` in favor of :func:`is_extension_array_dtype` (:issue:`29457`) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 247126227c587..b08a0d3a60526 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -518,7 +518,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. @@ -526,27 +526,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 @@ -564,35 +543,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(), - ) - 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(), - ) - else: - keep_tz = True - - if keep_tz and 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) diff --git a/pandas/tests/frame/indexing/test_setitem.py b/pandas/tests/frame/indexing/test_setitem.py index df10aaf416162..6e7ba57dddf8f 100644 --- a/pandas/tests/frame/indexing/test_setitem.py +++ b/pandas/tests/frame/indexing/test_setitem.py @@ -779,11 +779,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)