Skip to content

Commit 10d2c8a

Browse files
committed
refactor construct_from_string
1 parent c14b45f commit 10d2c8a

File tree

2 files changed

+9
-5
lines changed

2 files changed

+9
-5
lines changed

pandas/core/dtypes/dtypes.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -569,16 +569,16 @@ def construct_from_string(cls, string):
569569
>>> DatetimeTZDtype.construct_from_string('datetime64[ns, UTC]')
570570
datetime64[ns, UTC]
571571
"""
572-
msg = "could not construct DatetimeTZDtype"""
572+
msg = "Could not construct DatetimeTZDtype from {}"
573573
try:
574574
match = cls._match.match(string)
575575
if match:
576576
d = match.groupdict()
577577
return cls(unit=d['unit'], tz=d['tz'])
578-
else:
579-
raise TypeError(msg)
580-
except ValueError:
581-
raise TypeError(msg)
578+
except Exception:
579+
# TODO(py3): Change this pass to `raise TypeError(msg) from e`
580+
pass
581+
raise TypeError(msg.format(string))
582582

583583
def __unicode__(self):
584584
return "datetime64[{unit}, {tz}]".format(unit=self.unit, tz=self.tz)

pandas/tests/dtypes/test_dtypes.py

+4
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,10 @@ def test_construction_from_string(self):
189189
pytest.raises(TypeError,
190190
lambda: DatetimeTZDtype.construct_from_string('foo'))
191191

192+
def test_construct_from_string_raises(self):
193+
with pytest.raises(TypeError, match="notatz"):
194+
DatetimeTZDtype.construct_from_string('datetime64[ns, notatz]')
195+
192196
def test_is_dtype(self):
193197
assert not DatetimeTZDtype.is_dtype(None)
194198
assert DatetimeTZDtype.is_dtype(self.dtype)

0 commit comments

Comments
 (0)