Skip to content

Commit

Permalink
refactor construct_from_string
Browse files Browse the repository at this point in the history
  • Loading branch information
TomAugspurger committed Nov 29, 2018
1 parent c14b45f commit 10d2c8a
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
10 changes: 5 additions & 5 deletions pandas/core/dtypes/dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -569,16 +569,16 @@ def construct_from_string(cls, string):
>>> DatetimeTZDtype.construct_from_string('datetime64[ns, UTC]')
datetime64[ns, UTC]
"""
msg = "could not construct DatetimeTZDtype"""
msg = "Could not construct DatetimeTZDtype from {}"
try:
match = cls._match.match(string)
if match:
d = match.groupdict()
return cls(unit=d['unit'], tz=d['tz'])
else:
raise TypeError(msg)
except ValueError:
raise TypeError(msg)
except Exception:
# TODO(py3): Change this pass to `raise TypeError(msg) from e`
pass
raise TypeError(msg.format(string))

def __unicode__(self):
return "datetime64[{unit}, {tz}]".format(unit=self.unit, tz=self.tz)
Expand Down
4 changes: 4 additions & 0 deletions pandas/tests/dtypes/test_dtypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,10 @@ def test_construction_from_string(self):
pytest.raises(TypeError,
lambda: DatetimeTZDtype.construct_from_string('foo'))

def test_construct_from_string_raises(self):
with pytest.raises(TypeError, match="notatz"):
DatetimeTZDtype.construct_from_string('datetime64[ns, notatz]')

def test_is_dtype(self):
assert not DatetimeTZDtype.is_dtype(None)
assert DatetimeTZDtype.is_dtype(self.dtype)
Expand Down

0 comments on commit 10d2c8a

Please sign in to comment.