Skip to content

Commit 2d95908

Browse files
authored
PERF: constructing string Series (#36317)
1 parent 22374c3 commit 2d95908

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

doc/source/whatsnew/v1.2.0.rst

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ Deprecations
205205
Performance improvements
206206
~~~~~~~~~~~~~~~~~~~~~~~~
207207

208-
- Performance improvements when creating Series with dtype `str` or :class:`StringDtype` from array with many string elements (:issue:`36304`)
208+
- Performance improvements when creating Series with dtype `str` or :class:`StringDtype` from array with many string elements (:issue:`36304`, :issue:`36317`)
209209
- Performance improvement in :meth:`GroupBy.agg` with the ``numba`` engine (:issue:`35759`)
210210
-
211211

pandas/core/construction.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
is_list_like,
3838
is_object_dtype,
3939
is_sparse,
40+
is_string_dtype,
4041
is_timedelta64_ns_dtype,
4142
)
4243
from pandas.core.dtypes.generic import (
@@ -510,7 +511,8 @@ def sanitize_array(
510511
data = np.array(data, dtype=dtype, copy=False)
511512
subarr = np.array(data, dtype=object, copy=copy)
512513

513-
if is_object_dtype(subarr.dtype) and not is_object_dtype(dtype):
514+
is_object_or_str_dtype = is_object_dtype(dtype) or is_string_dtype(dtype)
515+
if is_object_dtype(subarr.dtype) and not is_object_or_str_dtype:
514516
inferred = lib.infer_dtype(subarr, skipna=False)
515517
if inferred in {"interval", "period"}:
516518
subarr = array(subarr)

0 commit comments

Comments
 (0)