4444 soft_convert_objects ,
4545 maybe_convert_objects ,
4646 astype_nansafe ,
47- find_common_type )
47+ find_common_type ,
48+ maybe_infer_dtype_type )
4849from pandas .core .dtypes .missing import (
4950 isna , notna , array_equivalent ,
5051 _isna_compat ,
@@ -629,10 +630,9 @@ def convert(self, copy=True, **kwargs):
629630 def _can_hold_element (self , element ):
630631 """ require the same dtype as ourselves """
631632 dtype = self .values .dtype .type
632- if is_list_like (element ):
633- element = np .asarray (element )
634- tipo = element .dtype .type
635- return issubclass (tipo , dtype )
633+ tipo = maybe_infer_dtype_type (element )
634+ if tipo is not None :
635+ return issubclass (tipo .type , dtype )
636636 return isinstance (element , dtype )
637637
638638 def _try_cast_result (self , result , dtype = None ):
@@ -1806,11 +1806,10 @@ class FloatBlock(FloatOrComplexBlock):
18061806 _downcast_dtype = 'int64'
18071807
18081808 def _can_hold_element (self , element ):
1809- if is_list_like (element ):
1810- element = np .asarray (element )
1811- tipo = element .dtype .type
1812- return (issubclass (tipo , (np .floating , np .integer )) and
1813- not issubclass (tipo , (np .datetime64 , np .timedelta64 )))
1809+ tipo = maybe_infer_dtype_type (element )
1810+ if tipo is not None :
1811+ return (issubclass (tipo .type , (np .floating , np .integer )) and
1812+ not issubclass (tipo .type , (np .datetime64 , np .timedelta64 )))
18141813 return (isinstance (element , (float , int , np .floating , np .int_ )) and
18151814 not isinstance (element , (bool , np .bool_ , datetime , timedelta ,
18161815 np .datetime64 , np .timedelta64 )))
@@ -1856,9 +1855,9 @@ class ComplexBlock(FloatOrComplexBlock):
18561855 is_complex = True
18571856
18581857 def _can_hold_element (self , element ):
1859- if is_list_like (element ):
1860- element = np . array ( element )
1861- return issubclass (element . dtype .type ,
1858+ tipo = maybe_infer_dtype_type (element )
1859+ if tipo is not None :
1860+ return issubclass (tipo .type ,
18621861 (np .floating , np .integer , np .complexfloating ))
18631862 return (isinstance (element ,
18641863 (float , int , complex , np .float_ , np .int_ )) and
@@ -1874,12 +1873,12 @@ class IntBlock(NumericBlock):
18741873 _can_hold_na = False
18751874
18761875 def _can_hold_element (self , element ):
1877- if is_list_like (element ):
1878- element = np . array ( element )
1879- tipo = element . dtype . type
1880- return ( issubclass (tipo , np .integer ) and
1881- not issubclass ( tipo , ( np . datetime64 , np .timedelta64 )) and
1882- self .dtype .itemsize >= element . dtype .itemsize )
1876+ tipo = maybe_infer_dtype_type (element )
1877+ if tipo is not None :
1878+ return ( issubclass ( tipo . type , np . integer ) and
1879+ not issubclass (tipo . type , ( np .datetime64 ,
1880+ np .timedelta64 )) and
1881+ self .dtype .itemsize >= tipo .itemsize )
18831882 return is_integer (element )
18841883
18851884 def should_store (self , value ):
@@ -1917,10 +1916,9 @@ def _box_func(self):
19171916 return lambda x : tslib .Timedelta (x , unit = 'ns' )
19181917
19191918 def _can_hold_element (self , element ):
1920- if is_list_like (element ):
1921- element = np .array (element )
1922- tipo = element .dtype .type
1923- return issubclass (tipo , np .timedelta64 )
1919+ tipo = maybe_infer_dtype_type (element )
1920+ if tipo is not None :
1921+ return issubclass (tipo .type , np .timedelta64 )
19241922 return isinstance (element , (timedelta , np .timedelta64 ))
19251923
19261924 def fillna (self , value , ** kwargs ):
@@ -2018,9 +2016,9 @@ class BoolBlock(NumericBlock):
20182016 _can_hold_na = False
20192017
20202018 def _can_hold_element (self , element ):
2021- if is_list_like (element ):
2022- element = np . asarray ( element )
2023- return issubclass (element . dtype .type , np .bool_ )
2019+ tipo = maybe_infer_dtype_type (element )
2020+ if tipo is not None :
2021+ return issubclass (tipo .type , np .bool_ )
20242022 return isinstance (element , (bool , np .bool_ ))
20252023
20262024 def should_store (self , value ):
@@ -2450,7 +2448,9 @@ def _astype(self, dtype, mgr=None, **kwargs):
24502448 return super (DatetimeBlock , self )._astype (dtype = dtype , ** kwargs )
24512449
24522450 def _can_hold_element (self , element ):
2453- if is_list_like (element ):
2451+ tipo = maybe_infer_dtype_type (element )
2452+ if tipo is not None :
2453+ # TODO: this still uses asarray, instead of dtype.type
24542454 element = np .array (element )
24552455 return element .dtype == _NS_DTYPE or element .dtype == np .int64
24562456 return (is_integer (element ) or isinstance (element , datetime ) or
0 commit comments