Skip to content

Cython Unicode Method Cleanups #26138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 0 additions & 55 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -549,41 +549,6 @@ def astype_intsafe(ndarray[object] arr, new_dtype):
return result


@cython.wraparound(False)
@cython.boundscheck(False)
def astype_unicode(arr: ndarray, skipna: bool=False) -> ndarray[object]:
"""
Convert all elements in an array to unicode.

Parameters
----------
arr : ndarray
The array whose elements we are casting.
skipna : bool, default False
Whether or not to coerce nulls to their stringified form
(e.g. NaN becomes 'nan').

Returns
-------
casted_arr : ndarray
A new array with the input array's elements casted.
"""
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]

if not (skipna and checknull(arr_i)):
arr_i = unicode(arr_i)

result[i] = arr_i

return result


@cython.wraparound(False)
@cython.boundscheck(False)
def astype_str(arr: ndarray, skipna: bool=False) -> ndarray[object]:
Expand Down Expand Up @@ -1321,10 +1286,6 @@ def infer_dtype(value: object, skipna: object=None) -> str:
if is_string_array(values, skipna=skipna):
return 'string'

elif isinstance(val, unicode):
if is_unicode_array(values, skipna=skipna):
return 'unicode'

elif isinstance(val, bytes):
if is_bytes_array(values, skipna=skipna):
return 'bytes'
Expand Down Expand Up @@ -1596,22 +1557,6 @@ cpdef bint is_string_array(ndarray values, bint skipna=False):
return validator.validate(values)


cdef class UnicodeValidator(Validator):
cdef inline bint is_value_typed(self, object value) except -1:
return isinstance(value, unicode)

cdef inline bint is_array_typed(self) except -1:
return issubclass(self.dtype.type, np.unicode_)


cdef bint is_unicode_array(ndarray values, bint skipna=False):
cdef:
UnicodeValidator validator = UnicodeValidator(len(values),
values.dtype,
skipna=skipna)
return validator.validate(values)


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

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

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

Expand Down