Skip to content

Commit c110f27

Browse files
authored
DEPR: Enforce DTI.to_series(keep_tz) removal (#48823)
1 parent 0ecd0ec commit c110f27

File tree

4 files changed

+6
-80
lines changed

4 files changed

+6
-80
lines changed

doc/source/whatsnew/v2.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ Deprecations
144144

145145
Removal of prior version deprecations/changes
146146
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
147+
- Removed ``keep_tz`` argument in :meth:`DatetimeIndex.to_series` (:issue:`29731`)
147148
- Remove arguments ``names`` and ``dtype`` from :meth:`Index.copy` and ``levels`` and ``codes`` from :meth:`MultiIndex.copy` (:issue:`35853`, :issue:`36685`)
148149
- Disallow passing non-round floats to :class:`Timestamp` with ``unit="M"`` or ``unit="Y"`` (:issue:`47266`)
149150
- Removed the ``numeric_only`` keyword from :meth:`Categorical.min` and :meth:`Categorical.max` in favor of ``skipna`` (:issue:`48821`)

pandas/core/indexes/datetimes.py

+2-51
Original file line numberDiff line numberDiff line change
@@ -518,35 +518,14 @@ def _get_time_micros(self) -> npt.NDArray[np.int64]:
518518
micros[self._isnan] = -1
519519
return micros
520520

521-
def to_series(self, keep_tz=lib.no_default, index=None, name=None):
521+
def to_series(self, index=None, name=None):
522522
"""
523523
Create a Series with both index and values equal to the index keys.
524524
525525
Useful with map for returning an indexer based on an index.
526526
527527
Parameters
528528
----------
529-
keep_tz : optional, defaults True
530-
Return the data keeping the timezone.
531-
532-
If keep_tz is True:
533-
534-
If the timezone is not set, the resulting
535-
Series will have a datetime64[ns] dtype.
536-
537-
Otherwise the Series will have an datetime64[ns, tz] dtype; the
538-
tz will be preserved.
539-
540-
If keep_tz is False:
541-
542-
Series will have a datetime64[ns] dtype. TZ aware
543-
objects will have the tz removed.
544-
545-
.. versionchanged:: 1.0.0
546-
The default value is now True. In a future version,
547-
this keyword will be removed entirely. Stop passing the
548-
argument to obtain the future behavior and silence the warning.
549-
550529
index : Index, optional
551530
Index of resulting Series. If None, defaults to original index.
552531
name : str, optional
@@ -564,35 +543,7 @@ def to_series(self, keep_tz=lib.no_default, index=None, name=None):
564543
if name is None:
565544
name = self.name
566545

567-
if keep_tz is not lib.no_default:
568-
if keep_tz:
569-
warnings.warn(
570-
"The 'keep_tz' keyword in DatetimeIndex.to_series "
571-
"is deprecated and will be removed in a future version. "
572-
"You can stop passing 'keep_tz' to silence this warning.",
573-
FutureWarning,
574-
stacklevel=find_stack_level(),
575-
)
576-
else:
577-
warnings.warn(
578-
"Specifying 'keep_tz=False' is deprecated and this "
579-
"option will be removed in a future release. If "
580-
"you want to remove the timezone information, you "
581-
"can do 'idx.tz_convert(None)' before calling "
582-
"'to_series'.",
583-
FutureWarning,
584-
stacklevel=find_stack_level(),
585-
)
586-
else:
587-
keep_tz = True
588-
589-
if keep_tz and self.tz is not None:
590-
# preserve the tz & copy
591-
values = self.copy(deep=True)
592-
else:
593-
# error: Incompatible types in assignment (expression has type
594-
# "Union[ExtensionArray, ndarray]", variable has type "DatetimeIndex")
595-
values = self._values.view("M8[ns]").copy() # type: ignore[assignment]
546+
values = self._values.copy()
596547

597548
return Series(values, index=index, name=name)
598549

pandas/tests/frame/indexing/test_setitem.py

+1-5
Original file line numberDiff line numberDiff line change
@@ -779,11 +779,7 @@ def test_setitem_dt64series(self, idx, expected):
779779
# convert to utc
780780
df = DataFrame(np.random.randn(2, 1), columns=["A"])
781781
df["B"] = idx
782-
783-
with tm.assert_produces_warning(FutureWarning) as m:
784-
df["B"] = idx.to_series(keep_tz=False, index=[0, 1])
785-
msg = "do 'idx.tz_convert(None)' before calling"
786-
assert msg in str(m[0].message)
782+
df["B"] = idx.to_series(index=[0, 1]).dt.tz_convert(None)
787783

788784
result = df["B"]
789785
comp = Series(idx.tz_convert("UTC").tz_localize(None), name="B")
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import numpy as np
2-
import pytest
32

43
from pandas import (
54
DatetimeIndex,
@@ -9,32 +8,11 @@
98

109

1110
class TestToSeries:
12-
@pytest.fixture
13-
def idx_expected(self):
11+
def test_to_series(self):
1412
naive = DatetimeIndex(["2013-1-1 13:00", "2013-1-2 14:00"], name="B")
1513
idx = naive.tz_localize("US/Pacific")
1614

1715
expected = Series(np.array(idx.tolist(), dtype="object"), name="B")
18-
16+
result = idx.to_series(index=[0, 1])
1917
assert expected.dtype == idx.dtype
20-
return idx, expected
21-
22-
def test_to_series_keep_tz_deprecated_true(self, idx_expected):
23-
# convert to series while keeping the timezone
24-
idx, expected = idx_expected
25-
26-
msg = "stop passing 'keep_tz'"
27-
with tm.assert_produces_warning(FutureWarning) as m:
28-
result = idx.to_series(keep_tz=True, index=[0, 1])
29-
assert msg in str(m[0].message)
30-
3118
tm.assert_series_equal(result, expected)
32-
33-
def test_to_series_keep_tz_deprecated_false(self, idx_expected):
34-
idx, expected = idx_expected
35-
36-
with tm.assert_produces_warning(FutureWarning) as m:
37-
result = idx.to_series(keep_tz=False, index=[0, 1])
38-
tm.assert_series_equal(result, expected.dt.tz_convert(None))
39-
msg = "do 'idx.tz_convert(None)' before calling"
40-
assert msg in str(m[0].message)

0 commit comments

Comments
 (0)