Skip to content

Commit

Permalink
Backport PR #60461: PERF: improve construct_1d_object_array_from_list…
Browse files Browse the repository at this point in the history
…like
  • Loading branch information
jorisvandenbossche authored and meeseeksmachine committed Dec 3, 2024
1 parent 2b37c98 commit 2df238e
Showing 1 changed file with 5 additions and 7 deletions.
12 changes: 5 additions & 7 deletions pandas/core/dtypes/cast.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,8 @@

if TYPE_CHECKING:
from collections.abc import (
Collection,
Sequence,
Sized,
)

from pandas._typing import (
Expand Down Expand Up @@ -1586,7 +1586,7 @@ def _maybe_box_and_unbox_datetimelike(value: Scalar, dtype: DtypeObj):
return _maybe_unbox_datetimelike(value, dtype)


def construct_1d_object_array_from_listlike(values: Sized) -> np.ndarray:
def construct_1d_object_array_from_listlike(values: Collection) -> np.ndarray:
"""
Transform any list-like object in a 1-dimensional numpy array of object
dtype.
Expand All @@ -1604,11 +1604,9 @@ def construct_1d_object_array_from_listlike(values: Sized) -> np.ndarray:
-------
1-dimensional numpy array of dtype object
"""
# numpy will try to interpret nested lists as further dimensions, hence
# making a 1D array that contains list-likes is a bit tricky:
result = np.empty(len(values), dtype="object")
result[:] = values
return result
# numpy will try to interpret nested lists as further dimensions in np.array(),
# hence explicitly making a 1D array using np.fromiter
return np.fromiter(values, dtype="object", count=len(values))


def maybe_cast_to_integer_array(arr: list | np.ndarray, dtype: np.dtype) -> np.ndarray:
Expand Down

0 comments on commit 2df238e

Please sign in to comment.