Skip to content

PERF: pd.concat with EA-backed indexes #49128

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 5 commits into from
Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from 4 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
3 changes: 3 additions & 0 deletions asv_bench/benchmarks/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,6 @@ def time_setitem_list(self, multiple_chunks):

def time_setitem_slice(self, multiple_chunks):
self.array[::10] = "foo"

def time_tolist(self, multiple_chunks):
self.array.tolist()
34 changes: 34 additions & 0 deletions asv_bench/benchmarks/join_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

from pandas import (
DataFrame,
Index,
MultiIndex,
Series,
array,
Expand Down Expand Up @@ -92,6 +93,39 @@ def time_f_ordered(self, axis, ignore_index):
concat(self.frame_f, axis=axis, ignore_index=ignore_index)


class ConcatIndexDtype:

params = (
["datetime64[ns]", "int64", "Int64", "string[python]", "string[pyarrow]"],
[0, 1],
[True, False],
[True, False],
)
param_names = ["dtype", "axis", "sort", "is_monotonic"]

def setup(self, dtype, axis, sort, is_monotonic):
N = 10_000
if dtype == "datetime64[ns]":
vals = date_range("1970-01-01", periods=N)
elif dtype in ("int64", "Int64"):
vals = np.arange(N, dtype=np.int64)
elif dtype in ("string[python]", "string[pyarrow]"):
vals = tm.makeStringIndex(N)
else:
raise NotImplementedError

idx = Index(vals, dtype=dtype)
if is_monotonic:
idx = idx.sort_values()
else:
idx = idx[::-1]

self.series = [Series(i, idx[i:]) for i in range(5)]

def time_concat_series(self, dtype, axis, sort, is_monotonic):
concat(self.series, axis=axis, sort=sort)


class Join:

params = [True, False]
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 @@ -154,6 +154,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 :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`)
- Performance improvement in ``var`` for nullable dtypes (:issue:`48379`).
Expand Down
3 changes: 2 additions & 1 deletion pandas/core/arrays/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,8 @@ def tolist(self) -> list:
"""
if self.ndim > 1:
return [x.tolist() for x in self]
return list(self)
# faster than list(self)
return self.to_numpy(dtype="object").tolist()

def delete(self: ExtensionArrayT, loc: PositionalIndexer) -> ExtensionArrayT:
indexer = np.delete(np.arange(len(self)), loc)
Expand Down
6 changes: 6 additions & 0 deletions pandas/tests/arrays/masked/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ def test_round(data, numpy_dtype):
dtype=data.dtype,
)
tm.assert_extension_array_equal(result, expected)


def test_tolist(data):
result = data.tolist()
expected = list(data)
tm.assert_equal(result, expected)
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 @@ -611,3 +611,11 @@ def test_setitem_scalar_with_mask_validation(dtype):
msg = "Scalar must be NA or str"
with pytest.raises(ValueError, match=msg):
ser[mask] = 1


def test_tolist(dtype):
vals = ["a", "b", "c"]
arr = pd.array(vals, dtype=dtype)
result = arr.tolist()
expected = vals
tm.assert_equal(result, expected)