Skip to content

Commit 142ef14

Browse files
jorisvandenbosschePingviinituutti
authored andcommitted
DEPR: deprecate default of keep_tz=False of DatetimeIndex.to_series (pandas-dev#23739)
1 parent 73d8284 commit 142ef14

File tree

3 files changed

+33
-3
lines changed

3 files changed

+33
-3
lines changed

doc/source/whatsnew/v0.24.0.rst

+2
Original file line numberDiff line numberDiff line change
@@ -1029,6 +1029,8 @@ Deprecations
10291029
`use_threads` to reflect the changes in pyarrow 0.11.0. (:issue:`23053`)
10301030
- :func:`pandas.read_excel` has deprecated accepting ``usecols`` as an integer. Please pass in a list of ints from 0 to ``usecols`` inclusive instead (:issue:`23527`)
10311031
- Constructing a :class:`TimedeltaIndex` from data with ``datetime64``-dtyped data is deprecated, will raise ``TypeError`` in a future version (:issue:`23539`)
1032+
- The ``keep_tz=False`` option (the default) of the ``keep_tz`` keyword of
1033+
:meth:`DatetimeIndex.to_series` is deprecated (:issue:`17832`).
10321034

10331035
.. _whatsnew_0240.deprecations.datetimelike_int_ops:
10341036

pandas/core/indexes/datetimes.py

+21-2
Original file line numberDiff line numberDiff line change
@@ -478,15 +478,15 @@ def _get_time_micros(self):
478478
values = self._local_timestamps()
479479
return fields.get_time_micros(values)
480480

481-
def to_series(self, keep_tz=False, index=None, name=None):
481+
def to_series(self, keep_tz=None, index=None, name=None):
482482
"""
483483
Create a Series with both index and values equal to the index keys
484484
useful with map for returning an indexer based on an index
485485
486486
Parameters
487487
----------
488488
keep_tz : optional, defaults False
489-
return the data keeping the timezone.
489+
Return the data keeping the timezone.
490490
491491
If keep_tz is True:
492492
@@ -500,6 +500,12 @@ def to_series(self, keep_tz=False, index=None, name=None):
500500
501501
Series will have a datetime64[ns] dtype. TZ aware
502502
objects will have the tz removed.
503+
504+
.. versionchanged:: 0.24
505+
The default value will change to True in a future release.
506+
You can set ``keep_tz=True`` to already obtain the future
507+
behaviour and silence the warning.
508+
503509
index : Index, optional
504510
index of resulting Series. If None, defaults to original index
505511
name : string, optional
@@ -517,6 +523,19 @@ def to_series(self, keep_tz=False, index=None, name=None):
517523
if name is None:
518524
name = self.name
519525

526+
if keep_tz is None and self.tz is not None:
527+
warnings.warn("The default of the 'keep_tz' keyword will change "
528+
"to True in a future release. You can set "
529+
"'keep_tz=True' to obtain the future behaviour and "
530+
"silence this warning.", FutureWarning, stacklevel=2)
531+
keep_tz = False
532+
elif keep_tz is False:
533+
warnings.warn("Specifying 'keep_tz=False' is deprecated and this "
534+
"option will be removed in a future release. If "
535+
"you want to remove the timezone information, you "
536+
"can do 'idx.tz_convert(None)' before calling "
537+
"'to_series'.", FutureWarning, stacklevel=2)
538+
520539
if keep_tz and self.tz is not None:
521540
# preserve the tz & copy
522541
values = self.copy(deep=True)

pandas/tests/frame/test_alter_axes.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -317,12 +317,21 @@ def test_convert_dti_to_series(self):
317317
tm.assert_series_equal(result, expected)
318318

319319
# convert to utc
320-
df['B'] = idx.to_series(index=[0, 1])
320+
with tm.assert_produces_warning(FutureWarning):
321+
df['B'] = idx.to_series(keep_tz=False, index=[0, 1])
321322
result = df['B']
322323
comp = Series(DatetimeIndex(expected.values).tz_localize(None),
323324
name='B')
324325
tm.assert_series_equal(result, comp)
325326

327+
with tm.assert_produces_warning(FutureWarning):
328+
result = idx.to_series(index=[0, 1])
329+
tm.assert_series_equal(result, expected.dt.tz_convert(None))
330+
331+
with tm.assert_produces_warning(FutureWarning):
332+
result = idx.to_series(keep_tz=False, index=[0, 1])
333+
tm.assert_series_equal(result, expected.dt.tz_convert(None))
334+
326335
# list of datetimes with a tz
327336
df['B'] = idx.to_pydatetime()
328337
result = df['B']

0 commit comments

Comments
 (0)