Skip to content

Commit

Permalink
delete libs_.lib.astype_str
Browse files Browse the repository at this point in the history
  • Loading branch information
topper-123 committed Aug 4, 2020
1 parent 6ca4645 commit 1337e7e
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 60 deletions.
93 changes: 34 additions & 59 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -618,35 +618,52 @@ def astype_intsafe(ndarray[object] arr, new_dtype):

@cython.wraparound(False)
@cython.boundscheck(False)
def astype_str(arr: ndarray, skipna: bool=False) -> ndarray[object]:
"""
Convert all elements in an array to string.
cpdef ndarray[object] ensure_string_array(
ndarray[object] arr,
object na_value=np.nan,
bint convert_na_value=True,
bint copy=True,
bint skipna=True,
):
"""Returns a new numpy array with object dtype and only strings and na values.
Parameters
----------
arr : ndarray
The array whose elements we are casting.
skipna : bool, default False
arr : array-like
The values to be converted to str, if needed.
na_value : Any
The value to use for na. For example, np.nan or pd.NA.
convert_na_value : bool, default True
If False, existing na values will be used unchanged in the new array.
copy : bool, default True
Whether to ensure that a new array is returned.
skipna : bool, default True
Whether or not to coerce nulls to their stringified form
(e.g. NaN becomes 'nan').
(e.g. if False, NaN becomes 'nan').
Returns
-------
ndarray
A new array with the input array's elements casted.
An array with the input array's elements casted to str or nan-like.
"""
cdef:
object arr_i
Py_ssize_t i, n = arr.size
ndarray[object] result = np.empty(n, dtype=object)

for i in range(n):
arr_i = arr[i]
Py_ssize_t i = 0, n = len(arr)

if not (skipna and checknull(arr_i)):
arr_i = str(arr_i)
result = np.asarray(arr, dtype="object")
if copy and result is arr:
result = result.copy()

result[i] = arr_i
for i in range(n):
val = result[i]
if not checknull(val):
result[i] = str(val)
else:
if convert_na_value:
val = na_value
if skipna:
result[i] = val
else:
result[i] = str(val)

return result

Expand Down Expand Up @@ -1698,48 +1715,6 @@ cpdef bint is_string_array(ndarray values, bint skipna=False):
return validator.validate(values)


cpdef ndarray ensure_string_array(
values, object na_value=np.nan, bint convert_na_value=True, bint copy=True):
"""Returns a new numpy array with object dtype and only strings and na values.
Parameters
----------
values : array-like
The values to be converted to str, if needed.
na_value : Any
The value to use for na. For example, np.nan or pd.NA.
convert_na_value : bool, default True
If False, existing na values will be used unchanged in the new array.
copy : bool, default True
Whether to ensure that a new array is returned.
Returns
-------
ndarray
"""
cdef:
Py_ssize_t i = 0, n = len(values)

result = np.asarray(values, dtype="object")
if copy and result is values:
result = result.copy()

if convert_na_value:
for i in range(n):
val = result[i]
if not checknull(val):
result[i] = str(val)
else:
result[i] = na_value
else:
for i in range(n):
val = result[i]
if not checknull(val):
result[i] = str(val)

return result


cdef class BytesValidator(Validator):
cdef inline bint is_value_typed(self, object value) except -1:
return isinstance(value, bytes)
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -916,7 +916,7 @@ def astype_nansafe(arr, dtype, copy: bool = True, skipna: bool = False):
dtype = pandas_dtype(dtype)

if issubclass(dtype.type, str):
return lib.astype_str(arr.ravel(), skipna=skipna).reshape(arr.shape)
return lib.ensure_string_array(arr.ravel(), skipna=skipna).reshape(arr.shape)

elif is_datetime64_dtype(arr):
if is_object_dtype(dtype):
Expand Down Expand Up @@ -1610,6 +1610,7 @@ def construct_1d_ndarray_preserving_na(
"""

if dtype is not None and dtype.kind == "U":
values = np.asarray(values, dtype="object")
subarr = lib.ensure_string_array(values, convert_na_value=False, copy=copy)
else:
subarr = np.array(values, dtype=dtype, copy=copy)
Expand Down

0 comments on commit 1337e7e

Please sign in to comment.