Skip to content

Commit 9179081

Browse files
authored
PERF: improve perf of conversion to string arrays (#43805)
1 parent dfe958c commit 9179081

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

doc/source/whatsnew/v1.4.0.rst

+1
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ Other Deprecations
347347
Performance improvements
348348
~~~~~~~~~~~~~~~~~~~~~~~~
349349
- Performance improvement in :meth:`.GroupBy.sample`, especially when ``weights`` argument provided (:issue:`34483`)
350+
- Performance improvement when converting non-string arrays to string arrays (:issue:`34483`)
350351
- Performance improvement in :meth:`.GroupBy.transform` for user-defined functions (:issue:`41598`)
351352
- Performance improvement in constructing :class:`DataFrame` objects (:issue:`42631`)
352353
- Performance improvement in :meth:`GroupBy.shift` when ``fill_value`` argument is provided (:issue:`26615`)

pandas/_libs/lib.pyx

+7-2
Original file line numberDiff line numberDiff line change
@@ -727,14 +727,19 @@ cpdef ndarray[object] ensure_string_array(
727727
continue
728728

729729
if not checknull(val):
730-
result[i] = str(val)
730+
if not isinstance(val, np.floating):
731+
# f"{val}" is faster than str(val)
732+
result[i] = f"{val}"
733+
else:
734+
# f"{val}" is not always equivalent to str(val) for floats
735+
result[i] = str(val)
731736
else:
732737
if convert_na_value:
733738
val = na_value
734739
if skipna:
735740
result[i] = val
736741
else:
737-
result[i] = str(val)
742+
result[i] = f"{val}"
738743

739744
return result
740745

0 commit comments

Comments
 (0)