Skip to content

DEPR: Enforce DTI.to_series(keep_tz) removal #48823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Oct 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 2 additions & 45 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -519,35 +519,14 @@ 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.

Useful with map for returning an indexer based on an index.

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
Expand All @@ -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:
Expand Down
6 changes: 1 addition & 5 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
26 changes: 2 additions & 24 deletions pandas/tests/indexes/datetimes/methods/test_to_series.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import numpy as np
import pytest

from pandas import (
DatetimeIndex,
Expand All @@ -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)