@@ -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+ 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
0 commit comments