8000 REF: StringArray._from_sequence, use less memory by topper-123 · Pull Request #35519 · pandas-dev/pandas · GitHub
[go: up one dir, main page]

Skip to content

REF: StringArray._from_sequence, use less memory #35519

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
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
delete libs_.lib.astype_str
  • Loading branch information
topper-123 committed Aug 16, 2020
commit ce18bb9f15905c01c4b1574862ca70beef839784
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")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont' actually need the np.asarray call here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok.

subarr = lib.ensure_string_array(values, convert_na_value=False, copy=copy)
else:
subarr = np.array(values, dtype=dtype, copy=copy)
Expand Down
0