From 75291853c5c39af9b8fef54d4a86919300e901a1 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 16 Nov 2018 11:13:01 +0100 Subject: [PATCH 1/3] DEPR: deprecate default of keep_tz=False of DatetimeIndex.to_series --- doc/source/whatsnew/v0.24.0.rst | 2 ++ pandas/core/indexes/datetimes.py | 17 +++++++++++++++-- pandas/tests/frame/test_alter_axes.py | 7 ++++++- 3 files changed, 23 insertions(+), 3 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index 007f5b7feb060..0b2f3b68ac927 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1029,6 +1029,8 @@ Deprecations `use_threads` to reflect the changes in pyarrow 0.11.0. (:issue:`23053`) - :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`) - Constructing a :class:`TimedeltaIndex` from data with ``datetime64``-dtyped data is deprecated, will raise ``TypeError`` in a future version (:issue:`23539`) +- The default value of the ``keep_tz`` keyword of :meth:`DatetimeIndex.to_series` + is deprecated and will change from False to True in a future release (:issue:`17832`). .. _whatsnew_0240.deprecations.datetimelike_int_ops: diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index a1fed8b93fcbb..b5cbc245d363b 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -478,7 +478,7 @@ def _get_time_micros(self): values = self._local_timestamps() return fields.get_time_micros(values) - def to_series(self, keep_tz=False, index=None, name=None): + def to_series(self, keep_tz=None, 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 @@ -486,7 +486,7 @@ def to_series(self, keep_tz=False, index=None, name=None): Parameters ---------- keep_tz : optional, defaults False - return the data keeping the timezone. + Return the data keeping the timezone. If keep_tz is True: @@ -500,6 +500,12 @@ def to_series(self, keep_tz=False, index=None, name=None): Series will have a datetime64[ns] dtype. TZ aware objects will have the tz removed. + + .. versionchanged:: 0.24 + The default value will change to True in a future release. + You can set ``keep_tz=True`` to already obtain the future + behaviour and silence the warning. + index : Index, optional index of resulting Series. If None, defaults to original index name : string, optional @@ -517,6 +523,13 @@ def to_series(self, keep_tz=False, index=None, name=None): if name is None: name = self.name + if keep_tz is None and self.tz is not None: + warnings.warn("The default of the 'keep_tz' keyword will change " + "to True in a future release. You can set " + "'keep_tz=True' to obtain the future behaviour and " + "silence this warning.", + FutureWarning, stacklevel=2) + keep_tz = False if keep_tz and self.tz is not None: # preserve the tz & copy values = self.copy(deep=True) diff --git a/pandas/tests/frame/test_alter_axes.py b/pandas/tests/frame/test_alter_axes.py index 2b4d1e6f25c65..f1e84f38a610f 100644 --- a/pandas/tests/frame/test_alter_axes.py +++ b/pandas/tests/frame/test_alter_axes.py @@ -317,12 +317,17 @@ def test_convert_dti_to_series(self): tm.assert_series_equal(result, expected) # convert to utc - df['B'] = idx.to_series(index=[0, 1]) + df['B'] = idx.to_series(keep_tz=False, index=[0, 1]) result = df['B'] comp = Series(DatetimeIndex(expected.values).tz_localize(None), name='B') tm.assert_series_equal(result, comp) + with tm.assert_produces_warning(FutureWarning): + result = idx.to_series(index=[0, 1]) + tm.assert_series_equal( + result, expected.dt.tz_convert('UTC').dt.tz_localize(None)) + # list of datetimes with a tz df['B'] = idx.to_pydatetime() result = df['B'] From 4c53832d6fbd78f2604598be9ba3e2f64f287b2f Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 16 Nov 2018 14:18:40 +0100 Subject: [PATCH 2/3] also deprecate keep_tz=False --- doc/source/whatsnew/v0.24.0.rst | 4 ++-- pandas/core/indexes/datetimes.py | 10 ++++++++-- pandas/tests/frame/test_alter_axes.py | 10 +++++++--- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/doc/source/whatsnew/v0.24.0.rst b/doc/source/whatsnew/v0.24.0.rst index 0b2f3b68ac927..3991b42fb1afc 100644 --- a/doc/source/whatsnew/v0.24.0.rst +++ b/doc/source/whatsnew/v0.24.0.rst @@ -1029,8 +1029,8 @@ Deprecations `use_threads` to reflect the changes in pyarrow 0.11.0. (:issue:`23053`) - :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`) - Constructing a :class:`TimedeltaIndex` from data with ``datetime64``-dtyped data is deprecated, will raise ``TypeError`` in a future version (:issue:`23539`) -- The default value of the ``keep_tz`` keyword of :meth:`DatetimeIndex.to_series` - is deprecated and will change from False to True in a future release (:issue:`17832`). +- The ``keep_tz=False`` option (the default) of the ``keep_tz`` keyword of + :meth:`DatetimeIndex.to_series` is deprecated (:issue:`17832`). .. _whatsnew_0240.deprecations.datetimelike_int_ops: diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index b5cbc245d363b..4a07d2dd3cb81 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -527,9 +527,15 @@ def to_series(self, keep_tz=None, index=None, name=None): warnings.warn("The default of the 'keep_tz' keyword will change " "to True in a future release. You can set " "'keep_tz=True' to obtain the future behaviour and " - "silence this warning.", - FutureWarning, stacklevel=2) + "silence this warning.", FutureWarning, stacklevel=2) keep_tz = False + elif not keep_tz: + 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=2) + if keep_tz and self.tz is not None: # preserve the tz & copy values = self.copy(deep=True) diff --git a/pandas/tests/frame/test_alter_axes.py b/pandas/tests/frame/test_alter_axes.py index f1e84f38a610f..33128a8ab179a 100644 --- a/pandas/tests/frame/test_alter_axes.py +++ b/pandas/tests/frame/test_alter_axes.py @@ -317,7 +317,8 @@ def test_convert_dti_to_series(self): tm.assert_series_equal(result, expected) # convert to utc - df['B'] = idx.to_series(keep_tz=False, index=[0, 1]) + with tm.assert_produces_warning(FutureWarning): + df['B'] = idx.to_series(keep_tz=False, index=[0, 1]) result = df['B'] comp = Series(DatetimeIndex(expected.values).tz_localize(None), name='B') @@ -325,8 +326,11 @@ def test_convert_dti_to_series(self): with tm.assert_produces_warning(FutureWarning): result = idx.to_series(index=[0, 1]) - tm.assert_series_equal( - result, expected.dt.tz_convert('UTC').dt.tz_localize(None)) + tm.assert_series_equal(result, expected.dt.tz_convert(None)) + + with tm.assert_produces_warning(FutureWarning): + result = idx.to_series(keep_tz=False, index=[0, 1]) + tm.assert_series_equal(result, expected.dt.tz_convert(None)) # list of datetimes with a tz df['B'] = idx.to_pydatetime() From d1390ed5455e2cd281dba6835427a4dfddab0007 Mon Sep 17 00:00:00 2001 From: Joris Van den Bossche Date: Fri, 16 Nov 2018 15:43:49 +0100 Subject: [PATCH 3/3] fix check for keep_tz is False --- pandas/core/indexes/datetimes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pandas/core/indexes/datetimes.py b/pandas/core/indexes/datetimes.py index 4a07d2dd3cb81..75f990096c677 100644 --- a/pandas/core/indexes/datetimes.py +++ b/pandas/core/indexes/datetimes.py @@ -529,7 +529,7 @@ def to_series(self, keep_tz=None, index=None, name=None): "'keep_tz=True' to obtain the future behaviour and " "silence this warning.", FutureWarning, stacklevel=2) keep_tz = False - elif not keep_tz: + elif keep_tz is False: 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 "