Skip to content

PERF: StringArray from np.str_ array #49109

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 6 commits into from
Oct 18, 2022
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: 18 additions & 0 deletions asv_bench/benchmarks/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,24 @@ def time_from_integer_array(self):
pd.array(self.values_integer, dtype="Int64")


class StringArray:
def setup(self):
N = 100_000
values = tm.rands_array(3, N)
self.values_obj = np.array(values, dtype="object")
self.values_str = np.array(values, dtype="U")
self.values_list = values.tolist()

def time_from_np_object_array(self):
pd.array(self.values_obj, dtype="string")

def time_from_np_str_array(self):
pd.array(self.values_str, dtype="string")

def time_from_list(self):
pd.array(self.values_list, dtype="string")


class ArrowStringArray:

params = [False, True]
Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v2.0.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ Performance improvements
- Performance improvement in :func:`merge` and :meth:`DataFrame.join` when joining on a sorted :class:`MultiIndex` (:issue:`48504`)
- Performance improvement in :meth:`DataFrame.loc` and :meth:`Series.loc` for tuple-based indexing of a :class:`MultiIndex` (:issue:`48384`)
- Performance improvement for :meth:`MultiIndex.unique` (:issue:`48335`)
- Performance improvement for :class:`~arrays.StringArray` constructor passing a numpy array with type ``np.str_`` (:issue:`49109`)
- Performance improvement for :func:`concat` with extension array backed indexes (:issue:`49128`)
- Performance improvement in :meth:`DataFrame.join` when joining on a subset of a :class:`MultiIndex` (:issue:`48611`)
- Performance improvement for :meth:`MultiIndex.intersection` (:issue:`48604`)
Expand Down
4 changes: 4 additions & 0 deletions pandas/_libs/lib.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,10 @@ cpdef ndarray[object] ensure_string_array(
if copy and result is arr:
result = result.copy()

if issubclass(arr.dtype.type, np.str_):
# short-circuit, all elements are str
return result

for i in range(n):
val = arr[i]

Expand Down
8 changes: 8 additions & 0 deletions pandas/tests/arrays/string_/test_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,14 @@ def test_setitem_scalar_with_mask_validation(dtype):
ser[mask] = 1


def test_from_numpy_str(dtype):
vals = ["a", "b", "c"]
arr = np.array(vals, dtype=np.str_)
result = pd.array(arr, dtype=dtype)
expected = pd.array(vals, dtype=dtype)
tm.assert_extension_array_equal(result, expected)


def test_tolist(dtype):
vals = ["a", "b", "c"]
arr = pd.array(vals, dtype=dtype)
Expand Down