Skip to content
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

DEPR: deprecate default of keep_tz=False of DatetimeIndex.to_series #23739

Merged
merged 3 commits into from
Nov 16, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions doc/source/whatsnew/v0.24.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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 ``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:

Expand Down
23 changes: 21 additions & 2 deletions pandas/core/indexes/datetimes.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,15 +478,15 @@ 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
Parameters
----------
keep_tz : optional, defaults False
return the data keeping the timezone.
Return the data keeping the timezone.
If keep_tz is True:
Expand All @@ -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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just remove the keyword in the future, we don't need to offer this additional funtionaility, this was mainly for backwards compat originally.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, my idea is to remove it in the future. But I think we first need to deprecate the behaviour, so people can switch, and once that is done, we can deprecate the full keyword.
See also some of the notes about it in #17832

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But, we can of course already discourage the 'False' behaviour, by also deprecating that (only allowing keep_tz=True

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah I think i would deprecate any non-None

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
Expand All @@ -517,6 +523,19 @@ 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
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 "
"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)
Expand Down
11 changes: 10 additions & 1 deletion pandas/tests/frame/test_alter_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,12 +317,21 @@ def test_convert_dti_to_series(self):
tm.assert_series_equal(result, expected)

# convert to utc
df['B'] = idx.to_series(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')
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(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()
result = df['B']
Expand Down