Skip to content

Commit 5c82eab

Browse files
committed
PERF: 3x speedup in Series of dicts with datetime keys by not having error message scale with input
1 parent fdc4db2 commit 5c82eab

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

pandas/core/dtypes/dtypes.py

+13-10
Original file line numberDiff line numberDiff line change
@@ -686,16 +686,19 @@ def construct_from_string(cls, string):
686686
>>> DatetimeTZDtype.construct_from_string('datetime64[ns, UTC]')
687687
datetime64[ns, UTC]
688688
"""
689-
msg = "Could not construct DatetimeTZDtype from '{}'"
690-
try:
691-
match = cls._match.match(string)
692-
if match:
693-
d = match.groupdict()
694-
return cls(unit=d['unit'], tz=d['tz'])
695-
except Exception:
696-
# TODO(py3): Change this pass to `raise TypeError(msg) from e`
697-
pass
698-
raise TypeError(msg.format(string))
689+
if isinstance(string, compat.string_types):
690+
msg = "Could not construct DatetimeTZDtype from '{}'"
691+
try:
692+
match = cls._match.match(string)
693+
if match:
694+
d = match.groupdict()
695+
return cls(unit=d['unit'], tz=d['tz'])
696+
except Exception:
697+
# TODO(py3): Change this pass to `raise TypeError(msg) from e`
698+
pass
699+
raise TypeError(msg.format(string))
700+
else:
701+
raise TypeError("Could not construct DatetimeTZDtype")
699702

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

0 commit comments

Comments
 (0)