Skip to content

Commit 2378e95

Browse files
qwhelanPingviinituutti
authored andcommitted
PERF: 3x speedup in Series of dicts with datetime keys by not having error message scale with input (pandas-dev#24743)
1 parent 27969a1 commit 2378e95

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-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+
701+
raise TypeError("Could not construct DatetimeTZDtype")
699702

700703
def __unicode__(self):
701704
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
@@ -232,6 +232,10 @@ def test_construct_from_string_raises(self):
232232
with pytest.raises(TypeError, match="notatz"):
233233
DatetimeTZDtype.construct_from_string('datetime64[ns, notatz]')
234234

235+
with pytest.raises(TypeError,
236+
match="^Could not construct DatetimeTZDtype$"):
237+
DatetimeTZDtype.construct_from_string(['datetime64[ns, notatz]'])
238+
235239
def test_is_dtype(self):
236240
assert not DatetimeTZDtype.is_dtype(None)
237241
assert DatetimeTZDtype.is_dtype(self.dtype)

0 commit comments

Comments
 (0)