Skip to content

REF/API: DatetimeTZDtype #23990

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

Merged
merged 20 commits into from
Dec 3, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Deprecate passing alias to unit
  • Loading branch information
TomAugspurger committed Dec 3, 2018
commit 22699f1d993896adfc72788ba7eece21a842dd01
19 changes: 16 additions & 3 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
""" define extension dtypes """
import re
import warnings

import numpy as np
import pytz
Expand Down Expand Up @@ -483,7 +484,6 @@ class DatetimeTZDtype(PandasExtensionDtype):
str = '|M8[ns]'
num = 101
base = np.dtype('M8[ns]')
na_value = NaT
_metadata = ('unit', 'tz')
_match = re.compile(r"(datetime64|M8)\[(?P<unit>.+), (?P<tz>.+)\]")
_cache = {}
Expand Down Expand Up @@ -517,7 +517,20 @@ def __init__(self, unit="ns", tz=None):
unit, tz = unit.unit, unit.tz

if unit != 'ns':
raise ValueError("DatetimeTZDtype only supports ns units")
if isinstance(unit, compat.string_types) and tz is None:
# maybe a string like datetime64[ns, tz], which we support for
# now.
result = type(self).construct_from_string(unit)
unit = result.unit
tz = result.tz
msg = (
"Passing a dtype alias like 'datetime64[ns, {tz}]' "
"to DatetimeTZDtype is deprecated. Use "
"'DatetimeTZDtype.construct_from_string()' instead."
)
warnings.warn(msg.format(tz=tz), FutureWarning, stacklevel=2)
else:
raise ValueError("DatetimeTZDtype only supports ns units")

if tz:
tz = timezones.maybe_get_tz(tz)
Expand Down Expand Up @@ -567,7 +580,7 @@ def construct_from_string(cls, string):
>>> DatetimeTZDtype.construct_from_string('datetime64[ns, UTC]')
datetime64[ns, UTC]
"""
msg = "Could not construct DatetimeTZDtype from {}"
msg = "Could not construct DatetimeTZDtype from '{}'"
try:
match = cls._match.match(string)
if match:
Expand Down
13 changes: 13 additions & 0 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,19 @@ class TestDatetimeTZDtype(Base):
def create(self):
return DatetimeTZDtype('ns', 'US/Eastern')

def test_alias_to_unit_raises(self):
# 23990
with tm.assert_produces_warning(FutureWarning):
DatetimeTZDtype('datetime64[ns, US/Central]')

def test_alias_to_unit_bad_alias_raises(self):
# 23990
with pytest.raises(TypeError, match=''):
DatetimeTZDtype('this is a bad string')

with pytest.raises(TypeError, match=''):
DatetimeTZDtype('datetime64[ns, US/NotATZ]')

def test_hash_vs_equality(self):
# make sure that we satisfy is semantics
dtype = self.dtype
Expand Down