File tree 4 files changed +17
-5
lines changed
4 files changed +17
-5
lines changed Original file line number Diff line number Diff line change @@ -276,10 +276,12 @@ def is_dtype(cls, dtype) -> bool:
276
276
return False
277
277
elif isinstance (dtype , cls ):
278
278
return True
279
- try :
280
- return cls .construct_from_string (dtype ) is not None
281
- except TypeError :
282
- return False
279
+ if isinstance (dtype , str ):
280
+ try :
281
+ return cls .construct_from_string (dtype ) is not None
282
+ except TypeError :
283
+ return False
284
+ return False
283
285
284
286
@property
285
287
def _is_numeric (self ) -> bool :
Original file line number Diff line number Diff line change @@ -882,7 +882,11 @@ def construct_from_string(cls, string):
882
882
return cls (freq = string )
883
883
except ValueError :
884
884
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 )
886
890
887
891
def __str__ (self ) -> str_type :
888
892
return self .name
Original file line number Diff line number Diff line change @@ -408,6 +408,9 @@ def test_construction_from_string(self):
408
408
with pytest .raises (TypeError ):
409
409
PeriodDtype .construct_from_string ("datetime64[ns, US/Eastern]" )
410
410
411
+ with pytest .raises (TypeError , match = "list" ):
412
+ PeriodDtype .construct_from_string ([1 , 2 , 3 ])
413
+
411
414
def test_is_dtype (self ):
412
415
assert PeriodDtype .is_dtype (self .dtype )
413
416
assert PeriodDtype .is_dtype ("period[D]" )
Original file line number Diff line number Diff line change @@ -37,6 +37,9 @@ def test_is_dtype_from_self(self, dtype):
37
37
result = type (dtype ).is_dtype (dtype )
38
38
assert result is True
39
39
40
+ def test_is_dtype_other_input (self , dtype ):
41
+ assert dtype .is_dtype ([1 , 2 , 3 ]) is False
42
+
40
43
def test_is_not_string_type (self , dtype ):
41
44
return not pd .api .types .is_string_dtype (dtype )
42
45
You can’t perform that action at this time.
0 commit comments