@@ -1777,42 +1777,20 @@ def hpat_pandas_series_astype(self, dtype, copy=True, errors='raise'):
1777
1777
errors in ('raise' , 'ignore' )):
1778
1778
ty_checker .raise_exc (errors , 'str' , 'errors' )
1779
1779
1780
- # Return StringArray for astype(str) or astype('str')
1781
- def hpat_pandas_series_astype_to_str_impl (self , dtype , copy = True , errors = 'raise' ):
1782
- num_chars = 0
1783
- arr_len = len (self ._data )
1784
-
1785
- # Get total chars for new array
1786
- for i in prange (arr_len ):
1787
- item = self ._data [i ]
1788
- num_chars += len (str (item )) # TODO: check NA
1789
-
1790
- data = pre_alloc_string_array (arr_len , num_chars )
1791
- for i in prange (arr_len ):
1792
- item = self ._data [i ]
1793
- data [i ] = str (item ) # TODO: check NA
1794
-
1795
- return pandas .Series (data = data , index = self ._index , name = self ._name )
1796
-
1797
1780
# Return npytypes.Array from npytypes.Array for astype(types.functions.NumberClass), example - astype(np.int64)
1798
- def hpat_pandas_series_astype_numba_impl (self , dtype , copy = True , errors = 'raise' ):
1799
- return pandas .Series (data = self ._data .astype (dtype ), index = self ._index , name = self ._name )
1800
-
1801
1781
# Return npytypes.Array from npytypes.Array for astype(types.StringLiteral), example - astype('int64')
1802
- def hpat_pandas_series_astype_literal_type_numba_impl (self , dtype , copy = True , errors = 'raise' ):
1803
- return pandas .Series (data = self . _data . astype (numpy . dtype ( dtype ) ), index = self ._index , name = self ._name )
1782
+ def hpat_pandas_series_astype_numba_impl (self , dtype , copy = True , errors = 'raise' ):
1783
+ return pandas .Series (data = numpy_like . astype (self . _data , dtype ), index = self ._index , name = self ._name )
1804
1784
1805
1785
# Return self
1806
1786
def hpat_pandas_series_astype_no_modify_impl (self , dtype , copy = True , errors = 'raise' ):
1807
1787
return pandas .Series (data = self ._data , index = self ._index , name = self ._name )
1808
1788
1809
-
1810
- if ((isinstance (dtype , types .Function ) and dtype .typing_key == str )
1811
- or (isinstance (dtype , types .StringLiteral ) and dtype .literal_value == 'str' )):
1812
- return hpat_pandas_series_astype_to_str_impl
1789
+ str_check = ((isinstance (dtype , types .Function ) and dtype .typing_key == str ) or
1790
+ (isinstance (dtype , types .StringLiteral ) and dtype .literal_value == 'str' ))
1813
1791
1814
1792
# Needs Numba astype impl support converting unicode_type to NumberClass and other types
1815
- if isinstance (self .data , StringArrayType ):
1793
+ if ( isinstance (self .data , StringArrayType ) and not str_check ):
1816
1794
if isinstance (dtype , types .functions .NumberClass ) and errors == 'raise' :
1817
1795
raise TypingError (f'Needs Numba astype impl support converting unicode_type to { dtype } ' )
1818
1796
if isinstance (dtype , types .StringLiteral ) and errors == 'raise' :
@@ -1823,18 +1801,12 @@ def hpat_pandas_series_astype_no_modify_impl(self, dtype, copy=True, errors='rai
1823
1801
else :
1824
1802
raise TypingError (f'Needs Numba astype impl support converting unicode_type to { dtype .literal_value } ' )
1825
1803
1826
- if isinstance (self .data , types .npytypes .Array ) and isinstance ( dtype , types . functions . NumberClass ):
1827
- return hpat_pandas_series_astype_numba_impl
1804
+ data_narr = isinstance (self .data , types .npytypes .Array )
1805
+ dtype_num_liter = isinstance ( dtype , ( types . functions . NumberClass , types . StringLiteral ))
1828
1806
1829
- if isinstance (self .data , types .npytypes .Array ) and isinstance (dtype , types .StringLiteral ):
1830
- try :
1831
- literal_value = numpy .dtype (dtype .literal_value )
1832
- except :
1833
- pass # Will raise the exception later
1834
- else :
1835
- return hpat_pandas_series_astype_literal_type_numba_impl
1807
+ if data_narr and dtype_num_liter or str_check :
1808
+ return hpat_pandas_series_astype_numba_impl
1836
1809
1837
- # Raise error if dtype is not supported
1838
1810
if errors == 'raise' :
1839
1811
raise TypingError (f'{ _func_name } The object must be a supported type. Given dtype: { dtype } ' )
1840
1812
else :
0 commit comments