diff --git a/doc/source/whatsnew/v1.4.0.rst b/doc/source/whatsnew/v1.4.0.rst index c8fa6d46c3b7e..986c10d824ba0 100644 --- a/doc/source/whatsnew/v1.4.0.rst +++ b/doc/source/whatsnew/v1.4.0.rst @@ -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`) diff --git a/pandas/_libs/lib.pyx b/pandas/_libs/lib.pyx index c9548a7e05fc5..4b1feab63dd1e 100644 --- a/pandas/_libs/lib.pyx +++ b/pandas/_libs/lib.pyx @@ -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