@@ -618,35 +618,52 @@ def astype_intsafe(ndarray[object] arr, new_dtype):
618618
619619@ cython.wraparound (False )
620620@ cython.boundscheck (False )
621- def astype_str (arr: ndarray , skipna: bool = False ) -> ndarray[object]:
622- """
623- Convert all elements in an array to string.
621+ cpdef ndarray[object ] ensure_string_array(
622+ ndarray[object ] arr,
623+ object na_value = np.nan,
624+ bint convert_na_value = True ,
625+ bint copy = True ,
626+ bint skipna = True ,
627+ ):
628+ """ Returns a new numpy array with object dtype and only strings and na values.
624629
625630 Parameters
626631 ----------
627- arr : ndarray
628- The array whose elements we are casting.
629- skipna : bool , default False
632+ arr : array-like
633+ The values to be converted to str, if needed.
634+ na_value : Any
635+ The value to use for na. For example, np.nan or pd.NA.
636+ convert_na_value : bool, default True
637+ If False, existing na values will be used unchanged in the new array.
638+ copy : bool, default True
639+ Whether to ensure that a new array is returned.
640+ skipna : bool, default True
630641 Whether or not to coerce nulls to their stringified form
631- (e.g. NaN becomes 'nan').
642+ (e.g. if False, NaN becomes 'nan').
632643
633644 Returns
634645 -------
635646 ndarray
636- A new array with the input array's elements casted.
647+ An array with the input array's elements casted to str or nan-like .
637648 """
638649 cdef:
639- object arr_i
640- Py_ssize_t i , n = arr.size
641- ndarray[object] result = np.empty(n, dtype = object )
642-
643- for i in range(n ):
644- arr_i = arr[i]
650+ Py_ssize_t i = 0 , n = len (arr)
645651
646- if not (skipna and checknull(arr_i)):
647- arr_i = str (arr_i)
652+ result = np.asarray(arr, dtype = " object" )
653+ if copy and result is arr:
654+ result = result.copy()
648655
649- result[i] = arr_i
656+ for i in range (n):
657+ val = result[i]
658+ if not checknull(val):
659+ result[i] = str (val)
660+ else :
661+ if convert_na_value:
662+ val = na_value
663+ if skipna:
664+ result[i] = val
665+ else :
666+ result[i] = str (val)
650667
651668 return result
652669
@@ -1698,48 +1715,6 @@ cpdef bint is_string_array(ndarray values, bint skipna=False):
16981715 return validator.validate(values)
16991716
17001717
1701- cpdef ndarray ensure_string_array(
1702- values, object na_value = np.nan, bint convert_na_value = True , bint copy = True ):
1703- """ Returns a new numpy array with object dtype and only strings and na values.
1704-
1705- Parameters
1706- ----------
1707- values : array-like
1708- The values to be converted to str, if needed.
1709- na_value : Any
1710- The value to use for na. For example, np.nan or pd.NA.
1711- convert_na_value : bool, default True
1712- If False, existing na values will be used unchanged in the new array.
1713- copy : bool, default True
1714- Whether to ensure that a new array is returned.
1715-
1716- Returns
1717- -------
1718- ndarray
1719- """
1720- cdef:
1721- Py_ssize_t i = 0 , n = len (values)
1722-
1723- result = np.asarray(values, dtype = " object" )
1724- if copy and result is values:
1725- result = result.copy()
1726-
1727- if convert_na_value:
1728- for i in range (n):
1729- val = result[i]
1730- if not checknull(val):
1731- result[i] = str (val)
1732- else :
1733- result[i] = na_value
1734- else :
1735- for i in range (n):
1736- val = result[i]
1737- if not checknull(val):
1738- result[i] = str (val)
1739-
1740- return result
1741-
1742-
17431718cdef class BytesValidator(Validator):
17441719 cdef inline bint is_value_typed(self , object value) except - 1 :
17451720 return isinstance (value, bytes)
0 commit comments