Skip to content

Commit dc9a20d

Browse files
committed
PERF: Fixed performance regression in Series init
Closes pandas-dev#30564
1 parent 733eb77 commit dc9a20d

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

pandas/core/dtypes/dtypes.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -882,7 +882,11 @@ def construct_from_string(cls, string):
882882
return cls(freq=string)
883883
except ValueError:
884884
pass
885-
raise TypeError(f"Cannot construct a 'PeriodDtype' from '{string}'")
885+
if isinstance(string, str):
886+
msg = f"Cannot construct a 'PeriodDtype' from '{string}'"
887+
else:
888+
msg = f"'construct_from_string' expects a string, got {type(string)}"
889+
raise TypeError(msg)
886890

887891
def __str__(self) -> str_type:
888892
return self.name

pandas/tests/dtypes/test_dtypes.py

+3
Original file line numberDiff line numberDiff line change
@@ -408,6 +408,9 @@ def test_construction_from_string(self):
408408
with pytest.raises(TypeError):
409409
PeriodDtype.construct_from_string("datetime64[ns, US/Eastern]")
410410

411+
with pytest.raises(TypeError, match="list"):
412+
PeriodDtype.construct_from_string([1, 2, 3])
413+
411414
def test_is_dtype(self):
412415
assert PeriodDtype.is_dtype(self.dtype)
413416
assert PeriodDtype.is_dtype("period[D]")

0 commit comments

Comments
 (0)