44
44
soft_convert_objects ,
45
45
maybe_convert_objects ,
46
46
astype_nansafe ,
47
- find_common_type )
47
+ find_common_type ,
48
+ _maybe_infer_dtype_type )
48
49
from pandas .core .dtypes .missing import (
49
50
isna , notna , array_equivalent ,
50
51
_isna_compat ,
@@ -629,9 +630,8 @@ def convert(self, copy=True, **kwargs):
629
630
def _can_hold_element (self , element ):
630
631
""" require the same dtype as ourselves """
631
632
dtype = self .values .dtype .type
632
- if is_list_like (element ):
633
- element = np .asarray (element )
634
- tipo = element .dtype .type
633
+ tipo = _maybe_infer_dtype_type (element )
634
+ if tipo :
635
635
return issubclass (tipo , dtype )
636
636
return isinstance (element , dtype )
637
637
@@ -1806,9 +1806,8 @@ class FloatBlock(FloatOrComplexBlock):
1806
1806
_downcast_dtype = 'int64'
1807
1807
1808
1808
def _can_hold_element (self , element ):
1809
- if is_list_like (element ):
1810
- element = np .asarray (element )
1811
- tipo = element .dtype .type
1809
+ tipo = _maybe_infer_dtype_type (element )
1810
+ if tipo :
1812
1811
return (issubclass (tipo , (np .floating , np .integer )) and
1813
1812
not issubclass (tipo , (np .datetime64 , np .timedelta64 )))
1814
1813
return (isinstance (element , (float , int , np .floating , np .int_ )) and
@@ -1856,9 +1855,9 @@ class ComplexBlock(FloatOrComplexBlock):
1856
1855
is_complex = True
1857
1856
1858
1857
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 :
1860
+ return issubclass (tipo ,
1862
1861
(np .floating , np .integer , np .complexfloating ))
1863
1862
return (isinstance (element ,
1864
1863
(float , int , complex , np .float_ , np .int_ )) and
@@ -1874,9 +1873,8 @@ class IntBlock(NumericBlock):
1874
1873
_can_hold_na = False
1875
1874
1876
1875
def _can_hold_element (self , element ):
1877
- if is_list_like (element ):
1878
- element = np .array (element )
1879
- tipo = element .dtype .type
1876
+ tipo = _maybe_infer_dtype_type (element )
1877
+ if tipo :
1880
1878
return (issubclass (tipo , np .integer ) and
1881
1879
not issubclass (tipo , (np .datetime64 , np .timedelta64 )) and
1882
1880
self .dtype .itemsize >= element .dtype .itemsize )
@@ -1917,9 +1915,8 @@ def _box_func(self):
1917
1915
return lambda x : tslib .Timedelta (x , unit = 'ns' )
1918
1916
1919
1917
def _can_hold_element (self , element ):
1920
- if is_list_like (element ):
1921
- element = np .array (element )
1922
- tipo = element .dtype .type
1918
+ tipo = _maybe_infer_dtype_type (element )
1919
+ if tipo :
1923
1920
return issubclass (tipo , np .timedelta64 )
1924
1921
return isinstance (element , (timedelta , np .timedelta64 ))
1925
1922
@@ -2018,9 +2015,9 @@ class BoolBlock(NumericBlock):
2018
2015
_can_hold_na = False
2019
2016
2020
2017
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_ )
2018
+ tipo = _maybe_infer_dtype_type (element )
2019
+ if tipo :
2020
+ return issubclass (tipo , np .bool_ )
2024
2021
return isinstance (element , (bool , np .bool_ ))
2025
2022
2026
2023
def should_store (self , value ):
@@ -2450,7 +2447,9 @@ def _astype(self, dtype, mgr=None, **kwargs):
2450
2447
return super (DatetimeBlock , self )._astype (dtype = dtype , ** kwargs )
2451
2448
2452
2449
def _can_hold_element (self , element ):
2453
- if is_list_like (element ):
2450
+ tipo = _maybe_infer_dtype_type (element )
2451
+ if tipo :
2452
+ # TODO: this still uses asarray, instead of dtype.type
2454
2453
element = np .array (element )
2455
2454
return element .dtype == _NS_DTYPE or element .dtype == np .int64
2456
2455
return (is_integer (element ) or isinstance (element , datetime ) or
0 commit comments