Skip to content

Commit 25980ad

Browse files
committed
PERF: construct DataFrame with string array and dtype=str
1 parent b4d0ae5 commit 25980ad

File tree

2 files changed

+12
-10
lines changed

2 files changed

+12
-10
lines changed

pandas/core/dtypes/cast.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1618,7 +1618,7 @@ def construct_1d_ndarray_preserving_na(
16181618
array(['1.0', '2.0', None], dtype=object)
16191619
"""
16201620

1621-
if dtype is not None and dtype.kind == "U":
1621+
if is_string_dtype(dtype):
16221622
subarr = lib.ensure_string_array(values, convert_na_value=False, copy=copy)
16231623
else:
16241624
subarr = np.array(values, dtype=dtype, copy=copy)

pandas/core/internals/construction.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
from pandas.core.dtypes.cast import (
1515
construct_1d_arraylike_from_scalar,
16+
construct_1d_ndarray_preserving_na,
1617
maybe_cast_to_datetime,
1718
maybe_convert_platform,
1819
maybe_infer_to_datetimelike,
@@ -189,15 +190,16 @@ def init_ndarray(values, index, columns, dtype: Optional[DtypeObj], copy: bool):
189190
# the dtypes will be coerced to a single dtype
190191
values = _prep_ndarray(values, copy=copy)
191192

192-
if dtype is not None:
193-
if not is_dtype_equal(values.dtype, dtype):
194-
try:
195-
values = values.astype(dtype)
196-
except Exception as orig:
197-
# e.g. ValueError when trying to cast object dtype to float64
198-
raise ValueError(
199-
f"failed to cast to '{dtype}' (Exception was: {orig})"
200-
) from orig
193+
if not is_dtype_equal(values.dtype, dtype):
194+
try:
195+
values = construct_1d_ndarray_preserving_na(
196+
values.ravel(), dtype=dtype, copy=False
197+
).reshape(values.shape)
198+
except Exception as orig:
199+
# e.g. ValueError when trying to cast object dtype to float64
200+
raise ValueError(
201+
f"failed to cast to '{dtype}' (Exception was: {orig})"
202+
) from orig
201203

202204
# _prep_ndarray ensures that values.ndim == 2 at this point
203205
index, columns = _get_axes(

0 commit comments

Comments
 (0)