diff --git a/pandas/core/dtypes/dtypes.py b/pandas/core/dtypes/dtypes.py index 79756d4c0cfab..f84471c3b04e8 100644 --- a/pandas/core/dtypes/dtypes.py +++ b/pandas/core/dtypes/dtypes.py @@ -686,16 +686,19 @@ def construct_from_string(cls, string): >>> DatetimeTZDtype.construct_from_string('datetime64[ns, UTC]') datetime64[ns, UTC] """ - 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']) - except Exception: - # TODO(py3): Change this pass to `raise TypeError(msg) from e` - pass - raise TypeError(msg.format(string)) + if isinstance(string, compat.string_types): + 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']) + except Exception: + # TODO(py3): Change this pass to `raise TypeError(msg) from e` + pass + raise TypeError(msg.format(string)) + + raise TypeError("Could not construct DatetimeTZDtype") def __unicode__(self): return "datetime64[{unit}, {tz}]".format(unit=self.unit, tz=self.tz) diff --git a/pandas/tests/dtypes/test_dtypes.py b/pandas/tests/dtypes/test_dtypes.py index 40b8f7afa3598..0fe0a845f5129 100644 --- a/pandas/tests/dtypes/test_dtypes.py +++ b/pandas/tests/dtypes/test_dtypes.py @@ -232,6 +232,10 @@ def test_construct_from_string_raises(self): with pytest.raises(TypeError, match="notatz"): DatetimeTZDtype.construct_from_string('datetime64[ns, notatz]') + with pytest.raises(TypeError, + match="^Could not construct DatetimeTZDtype$"): + DatetimeTZDtype.construct_from_string(['datetime64[ns, notatz]']) + def test_is_dtype(self): assert not DatetimeTZDtype.is_dtype(None) assert DatetimeTZDtype.is_dtype(self.dtype)