Skip to content

Commit b3b061b

Browse files
topper-123Kevin D Smith
authored and
Kevin D Smith
committed
PERF: StringArray construction (pandas-dev#36325)
1 parent 5151f29 commit b3b061b

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

doc/source/whatsnew/v1.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ Deprecations
221221
Performance improvements
222222
~~~~~~~~~~~~~~~~~~~~~~~~
223223

224-
- Performance improvements when creating Series with dtype `str` or :class:`StringDtype` from array with many string elements (:issue:`36304`, :issue:`36317`)
224+
- Performance improvements when creating Series with dtype `str` or :class:`StringDtype` from array with many string elements (:issue:`36304`, :issue:`36317`, :issue:`36325`)
225225
- Performance improvement in :meth:`GroupBy.agg` with the ``numba`` engine (:issue:`35759`)
226226
- Performance improvements when creating :meth:`pd.Series.map` from a huge dictionary (:issue:`34717`)
227227
- Performance improvement in :meth:`GroupBy.transform` with the ``numba`` engine (:issue:`36240`)

pandas/core/arrays/string_.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -199,13 +199,18 @@ def _from_sequence(cls, scalars, dtype=None, copy=False):
199199
assert dtype == "string"
200200

201201
result = np.asarray(scalars, dtype="object")
202-
203202
# convert non-na-likes to str, and nan-likes to StringDtype.na_value
204203
result = lib.ensure_string_array(
205204
result, na_value=StringDtype.na_value, copy=copy
206205
)
207206

208-
return cls(result)
207+
# Manually creating new array avoids the validation step in the __init__, so is
208+
# faster. Refactor need for validation?
209+
new_string_array = object.__new__(cls)
210+
new_string_array._dtype = StringDtype()
211+
new_string_array._ndarray = result
212+
213+
return new_string_array
209214

210215
@classmethod
211216
def _from_sequence_of_strings(cls, strings, dtype=None, copy=False):

0 commit comments

Comments
 (0)