Skip to content

Commit 9105c49

Browse files
jbrockmendeljacobaustin123
authored andcommitted
DEPR: change DTI.to_series keep_tz default to True (pandas-dev#29731)
1 parent 3065427 commit 9105c49

File tree

5 files changed

+43
-44
lines changed

5 files changed

+43
-44
lines changed

doc/source/whatsnew/v1.0.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,7 @@ or ``matplotlib.Axes.plot``. See :ref:`plotting.formatters` for more.
277277
- :meth:`pandas.Series.str.cat` does not accept list-likes *within* list-likes anymore (:issue:`27611`)
278278
- Removed the previously deprecated :meth:`ExtensionArray._formatting_values`. Use :attr:`ExtensionArray._formatter` instead. (:issue:`23601`)
279279
- Removed the previously deprecated ``IntervalIndex.from_intervals`` in favor of the :class:`IntervalIndex` constructor (:issue:`19263`)
280+
- Changed the default value for the "keep_tz" argument in :meth:`DatetimeIndex.to_series` to ``True`` (:issue:`23739`)
280281
- Ability to read pickles containing :class:`Categorical` instances created with pre-0.16 version of pandas has been removed (:issue:`27538`)
281282
- Removed the previously deprecated ``reduce`` and ``broadcast`` arguments from :meth:`DataFrame.apply` (:issue:`18577`)
282283
- Removed the previously deprecated ``assert_raises_regex`` function in ``pandas.util.testing`` (:issue:`29174`)

pandas/core/indexes/datetimes.py

+27-27
Original file line numberDiff line numberDiff line change
@@ -663,14 +663,14 @@ def _get_time_micros(self):
663663
values = self._data._local_timestamps()
664664
return fields.get_time_micros(values)
665665

666-
def to_series(self, keep_tz=None, index=None, name=None):
666+
def to_series(self, keep_tz=lib._no_default, index=None, name=None):
667667
"""
668668
Create a Series with both index and values equal to the index keys
669669
useful with map for returning an indexer based on an index.
670670
671671
Parameters
672672
----------
673-
keep_tz : optional, defaults False
673+
keep_tz : optional, defaults True
674674
Return the data keeping the timezone.
675675
676676
If keep_tz is True:
@@ -686,10 +686,10 @@ def to_series(self, keep_tz=None, index=None, name=None):
686686
Series will have a datetime64[ns] dtype. TZ aware
687687
objects will have the tz removed.
688688
689-
.. versionchanged:: 0.24
690-
The default value will change to True in a future release.
691-
You can set ``keep_tz=True`` to already obtain the future
692-
behaviour and silence the warning.
689+
.. versionchanged:: 1.0.0
690+
The default value is now True. In a future version,
691+
this keyword will be removed entirely. Stop passing the
692+
argument to obtain the future behavior and silence the warning.
693693
694694
index : Index, optional
695695
Index of resulting Series. If None, defaults to original index.
@@ -708,27 +708,27 @@ def to_series(self, keep_tz=None, index=None, name=None):
708708
if name is None:
709709
name = self.name
710710

711-
if keep_tz is None and self.tz is not None:
712-
warnings.warn(
713-
"The default of the 'keep_tz' keyword in "
714-
"DatetimeIndex.to_series will change "
715-
"to True in a future release. You can set "
716-
"'keep_tz=True' to obtain the future behaviour and "
717-
"silence this warning.",
718-
FutureWarning,
719-
stacklevel=2,
720-
)
721-
keep_tz = False
722-
elif keep_tz is False:
723-
warnings.warn(
724-
"Specifying 'keep_tz=False' is deprecated and this "
725-
"option will be removed in a future release. If "
726-
"you want to remove the timezone information, you "
727-
"can do 'idx.tz_convert(None)' before calling "
728-
"'to_series'.",
729-
FutureWarning,
730-
stacklevel=2,
731-
)
711+
if keep_tz is not lib._no_default:
712+
if keep_tz:
713+
warnings.warn(
714+
"The 'keep_tz' keyword in DatetimeIndex.to_series "
715+
"is deprecated and will be removed in a future version. "
716+
"You can stop passing 'keep_tz' to silence this warning.",
717+
FutureWarning,
718+
stacklevel=2,
719+
)
720+
else:
721+
warnings.warn(
722+
"Specifying 'keep_tz=False' is deprecated and this "
723+
"option will be removed in a future release. If "
724+
"you want to remove the timezone information, you "
725+
"can do 'idx.tz_convert(None)' before calling "
726+
"'to_series'.",
727+
FutureWarning,
728+
stacklevel=2,
729+
)
730+
else:
731+
keep_tz = True
732732

733733
if keep_tz and self.tz is not None:
734734
# preserve the tz & copy

pandas/tests/frame/test_alter_axes.py

+12-12
Original file line numberDiff line numberDiff line change
@@ -493,29 +493,29 @@ def test_convert_dti_to_series(self):
493493
tm.assert_series_equal(result, expected)
494494

495495
# convert to series while keeping the timezone
496-
result = idx.to_series(keep_tz=True, index=[0, 1])
496+
msg = "stop passing 'keep_tz'"
497+
with tm.assert_produces_warning(FutureWarning) as m:
498+
result = idx.to_series(keep_tz=True, index=[0, 1])
497499
tm.assert_series_equal(result, expected)
500+
assert msg in str(m[0].message)
498501

499502
# convert to utc
500-
with tm.assert_produces_warning(FutureWarning):
503+
with tm.assert_produces_warning(FutureWarning) as m:
501504
df["B"] = idx.to_series(keep_tz=False, index=[0, 1])
502505
result = df["B"]
503506
comp = Series(DatetimeIndex(expected.values).tz_localize(None), name="B")
504507
tm.assert_series_equal(result, comp)
505-
506-
with tm.assert_produces_warning(FutureWarning) as m:
507-
result = idx.to_series(index=[0, 1])
508-
tm.assert_series_equal(result, expected.dt.tz_convert(None))
509-
msg = (
510-
"The default of the 'keep_tz' keyword in "
511-
"DatetimeIndex.to_series will change to True in a future "
512-
"release."
513-
)
508+
msg = "do 'idx.tz_convert(None)' before calling"
514509
assert msg in str(m[0].message)
515510

516-
with tm.assert_produces_warning(FutureWarning):
511+
result = idx.to_series(index=[0, 1])
512+
tm.assert_series_equal(result, expected)
513+
514+
with tm.assert_produces_warning(FutureWarning) as m:
517515
result = idx.to_series(keep_tz=False, index=[0, 1])
518516
tm.assert_series_equal(result, expected.dt.tz_convert(None))
517+
msg = "do 'idx.tz_convert(None)' before calling"
518+
assert msg in str(m[0].message)
519519

520520
# list of datetimes with a tz
521521
df["B"] = idx.to_pydatetime()

pandas/tests/frame/test_constructors.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1795,7 +1795,7 @@ def test_constructor_with_datetimes(self):
17951795
# preserver an index with a tz on dict construction
17961796
i = date_range("1/1/2011", periods=5, freq="10s", tz="US/Eastern")
17971797

1798-
expected = DataFrame({"a": i.to_series(keep_tz=True).reset_index(drop=True)})
1798+
expected = DataFrame({"a": i.to_series().reset_index(drop=True)})
17991799
df = DataFrame()
18001800
df["a"] = i
18011801
tm.assert_frame_equal(df, expected)
@@ -1806,9 +1806,7 @@ def test_constructor_with_datetimes(self):
18061806
# multiples
18071807
i_no_tz = date_range("1/1/2011", periods=5, freq="10s")
18081808
df = DataFrame({"a": i, "b": i_no_tz})
1809-
expected = DataFrame(
1810-
{"a": i.to_series(keep_tz=True).reset_index(drop=True), "b": i_no_tz}
1811-
)
1809+
expected = DataFrame({"a": i.to_series().reset_index(drop=True), "b": i_no_tz})
18121810
tm.assert_frame_equal(df, expected)
18131811

18141812
def test_constructor_datetimes_with_nulls(self):

pandas/tests/test_base.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def setup_method(self, method):
179179
self.int_series = Series(arr, index=self.int_index, name="a")
180180
self.float_series = Series(arr, index=self.float_index, name="a")
181181
self.dt_series = Series(arr, index=self.dt_index, name="a")
182-
self.dt_tz_series = self.dt_tz_index.to_series(keep_tz=True)
182+
self.dt_tz_series = self.dt_tz_index.to_series()
183183
self.period_series = Series(arr, index=self.period_index, name="a")
184184
self.string_series = Series(arr, index=self.string_index, name="a")
185185
self.unicode_series = Series(arr, index=self.unicode_index, name="a")

0 commit comments

Comments
 (0)