Skip to content

Commit

Permalink
PERF: improve perf of conversion to string arrays (pandas-dev#43805)
Browse files Browse the repository at this point in the history
  • Loading branch information
topper-123 authored Sep 30, 2021
1 parent dfe958c commit 9179081
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.4.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,7 @@ Other Deprecations
Performance improvements
~~~~~~~~~~~~~~~~~~~~~~~~
- Performance improvement in :meth:`.GroupBy.sample`, especially when ``weights`` argument provided (:issue:`34483`)
- Performance improvement when converting non-string arrays to string arrays (:issue:`34483`)
- Performance improvement in :meth:`.GroupBy.transform` for user-defined functions (:issue:`41598`)
- Performance improvement in constructing :class:`DataFrame` objects (:issue:`42631`)
- Performance improvement in :meth:`GroupBy.shift` when ``fill_value`` argument is provided (:issue:`26615`)
Expand Down
9 changes: 7 additions & 2 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -727,14 +727,19 @@ cpdef ndarray[object] ensure_string_array(
continue

if not checknull(val):
result[i] = str(val)
if not isinstance(val, np.floating):
# f"{val}" is faster than str(val)
result[i] = f"{val}"
else:
# f"{val}" is not always equivalent to str(val) for floats
result[i] = str(val)
else:
if convert_na_value:
val = na_value
if skipna:
result[i] = val
else:
result[i] = str(val)
result[i] = f"{val}"

return result

Expand Down

0 comments on commit 9179081

Please sign in to comment.