Skip to content

PERF: ensure_string_array with non-numpy input array #37371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion asv_bench/benchmarks/strings.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import numpy as np

from pandas import DataFrame, Series
from pandas import Categorical, DataFrame, Series

from .pandas_vb_common import tm

Expand All @@ -16,6 +16,10 @@ def setup(self, dtype):
self.series_arr = tm.rands_array(nchars=10, size=10 ** 5)
self.frame_arr = self.series_arr.reshape((50_000, 2)).copy()

# GH37371. Testing construction of string series/frames from ExtensionArrays
self.series_cat_arr = Categorical(self.series_arr)
self.frame_cat_arr = Categorical(self.frame_arr)

def time_series_construction(self, dtype):
Series(self.series_arr, dtype=dtype)

Expand All @@ -28,6 +32,18 @@ def time_frame_construction(self, dtype):
def peakmem_frame_construction(self, dtype):
DataFrame(self.frame_arr, dtype=dtype)

def time_cat_series_construction(self, dtype):
Series(self.series_cat_arr, dtype=dtype)

def peakmem_cat_series_construction(self, dtype):
Series(self.series_cat_arr, dtype=dtype)

def time_cat_frame_construction(self, dtype):
DataFrame(self.frame_cat_arr, dtype=dtype)

def peakmem_cat_frame_construction(self, dtype):
DataFrame(self.frame_cat_arr, dtype=dtype)


class Methods:
def setup(self):
Expand Down
2 changes: 1 addition & 1 deletion doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ Deprecations
Performance improvements
~~~~~~~~~~~~~~~~~~~~~~~~

- Performance improvements when creating DataFrame or Series with dtype ``str`` or :class:`StringDtype` from array with many string elements (:issue:`36304`, :issue:`36317`, :issue:`36325`, :issue:`36432`)
- Performance improvements when creating DataFrame or Series with dtype ``str`` or :class:`StringDtype` from array with many string elements (:issue:`36304`, :issue:`36317`, :issue:`36325`, :issue:`36432`, :issue:`37371`)
- Performance improvement in :meth:`GroupBy.agg` with the ``numba`` engine (:issue:`35759`)
- Performance improvements when creating :meth:`pd.Series.map` from a huge dictionary (:issue:`34717`)
- Performance improvement in :meth:`GroupBy.transform` with the ``numba`` engine (:issue:`36240`)
Expand Down
5 changes: 5 additions & 0 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -651,6 +651,11 @@ cpdef ndarray[object] ensure_string_array(
cdef:
Py_ssize_t i = 0, n = len(arr)

if hasattr(arr, "to_numpy"):
arr = arr.to_numpy()
elif not isinstance(arr, np.ndarray):
arr = np.array(arr, dtype="object")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this should probably be asarray

not a big deal, but my preference would have been to type the input arr as an ndarray and handle non-ndarray in the calling function


result = np.asarray(arr, dtype="object")

if copy and result is arr:
Expand Down