@@ -629,9 +629,8 @@ def convert(self, copy=True, **kwargs):
629
629
def _can_hold_element (self , element ):
630
630
""" require the same dtype as ourselves """
631
631
dtype = self .values .dtype .type
632
- if is_list_like (element ):
633
- element = np .asarray (element )
634
- tipo = element .dtype .type
632
+ tipo = _maybe_get_element_dtype_type (element )
633
+ if tipo :
635
634
return issubclass (tipo , dtype )
636
635
return isinstance (element , dtype )
637
636
@@ -1806,9 +1805,8 @@ class FloatBlock(FloatOrComplexBlock):
1806
1805
_downcast_dtype = 'int64'
1807
1806
1808
1807
def _can_hold_element (self , element ):
1809
- if is_list_like (element ):
1810
- element = np .asarray (element )
1811
- tipo = element .dtype .type
1808
+ tipo = _maybe_get_element_dtype_type (element )
1809
+ if tipo :
1812
1810
return (issubclass (tipo , (np .floating , np .integer )) and
1813
1811
not issubclass (tipo , (np .datetime64 , np .timedelta64 )))
1814
1812
return (isinstance (element , (float , int , np .floating , np .int_ )) and
@@ -1856,9 +1854,9 @@ class ComplexBlock(FloatOrComplexBlock):
1856
1854
is_complex = True
1857
1855
1858
1856
def _can_hold_element (self , element ):
1859
- if is_list_like (element ):
1860
- element = np . array ( element )
1861
- return issubclass (element . dtype . type ,
1857
+ tipo = _maybe_get_element_dtype_type (element )
1858
+ if tipo :
1859
+ return issubclass (tipo ,
1862
1860
(np .floating , np .integer , np .complexfloating ))
1863
1861
return (isinstance (element ,
1864
1862
(float , int , complex , np .float_ , np .int_ )) and
@@ -1874,9 +1872,8 @@ class IntBlock(NumericBlock):
1874
1872
_can_hold_na = False
1875
1873
1876
1874
def _can_hold_element (self , element ):
1877
- if is_list_like (element ):
1878
- element = np .array (element )
1879
- tipo = element .dtype .type
1875
+ tipo = _maybe_get_element_dtype_type (element )
1876
+ if tipo :
1880
1877
return (issubclass (tipo , np .integer ) and
1881
1878
not issubclass (tipo , (np .datetime64 , np .timedelta64 )) and
1882
1879
self .dtype .itemsize >= element .dtype .itemsize )
@@ -1917,9 +1914,8 @@ def _box_func(self):
1917
1914
return lambda x : tslib .Timedelta (x , unit = 'ns' )
1918
1915
1919
1916
def _can_hold_element (self , element ):
1920
- if is_list_like (element ):
1921
- element = np .array (element )
1922
- tipo = element .dtype .type
1917
+ tipo = _maybe_get_element_dtype_type (element )
1918
+ if tipo :
1923
1919
return issubclass (tipo , np .timedelta64 )
1924
1920
return isinstance (element , (timedelta , np .timedelta64 ))
1925
1921
@@ -2018,9 +2014,9 @@ class BoolBlock(NumericBlock):
2018
2014
_can_hold_na = False
2019
2015
2020
2016
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_ )
2017
+ tipo = _maybe_get_element_dtype_type (element )
2018
+ if tipo :
2019
+ return issubclass (tipo , np .bool_ )
2024
2020
return isinstance (element , (bool , np .bool_ ))
2025
2021
2026
2022
def should_store (self , value ):
@@ -2450,7 +2446,9 @@ def _astype(self, dtype, mgr=None, **kwargs):
2450
2446
return super (DatetimeBlock , self )._astype (dtype = dtype , ** kwargs )
2451
2447
2452
2448
def _can_hold_element (self , element ):
2453
- if is_list_like (element ):
2449
+ tipo = _maybe_get_element_dtype_type (element )
2450
+ if tipo :
2451
+ # TODO: this still uses asarray, instead of dtype.type
2454
2452
element = np .array (element )
2455
2453
return element .dtype == _NS_DTYPE or element .dtype == np .int64
2456
2454
return (is_integer (element ) or isinstance (element , datetime ) or
@@ -5525,3 +5523,13 @@ def _preprocess_slice_or_indexer(slice_or_indexer, length, allow_fill):
5525
5523
if not allow_fill :
5526
5524
indexer = maybe_convert_indices (indexer , length )
5527
5525
return 'fancy' , indexer , len (indexer )
5526
+
5527
+
5528
+ def _maybe_get_element_dtype_type (element ):
5529
+ tipo = None
5530
+ if hasattr (element , 'dtype' ):
5531
+ tipo = element .dtype .type
5532
+ elif is_list_like (element ):
5533
+ element = np .asarray (element )
5534
+ tipo = element .dtype .type
5535
+ return tipo
0 commit comments