Skip to content
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

ENH: Improve performance for df.__setitem__ with list-like indexers #38148

Merged
merged 4 commits into from
Nov 29, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 4 additions & 0 deletions asv_bench/benchmarks/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,10 @@ def time_assign_with_setitem(self):
for i in range(100):
self.df[i] = np.random.randn(self.N)

def time_assign_list_like_with_setitem(self):
jreback marked this conversation as resolved.
Show resolved Hide resolved
np.random.seed(1234)
self.df[list(range(100))] = np.random.randn(self.N, 100)


class ChainIndexing:

Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ Other enhancements
- Improve numerical stability for :meth:`.Rolling.skew`, :meth:`.Rolling.kurt`, :meth:`Expanding.skew` and :meth:`Expanding.kurt` through implementation of Kahan summation (:issue:`6929`)
- Improved error reporting for subsetting columns of a :class:`.DataFrameGroupBy` with ``axis=1`` (:issue:`37725`)
- Implement method ``cross`` for :meth:`DataFrame.merge` and :meth:`DataFrame.join` (:issue:`5401`)
- Improve performance for :meth:`DataFrame.__setitem__` with list-like indexers (:issue:`37954`)
jreback marked this conversation as resolved.
Show resolved Hide resolved

.. ---------------------------------------------------------------------------

Expand Down
14 changes: 3 additions & 11 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -663,17 +663,9 @@ def _ensure_listlike_indexer(self, key, axis=None, value=None):
and not com.is_bool_indexer(key)
and all(is_hashable(k) for k in key)
):
for i, k in enumerate(key):
if k not in self.obj:
if value is None:
self.obj[k] = np.nan
elif is_array_like(value) and value.ndim == 2:
# GH#37964 have to select columnwise in case of array
self.obj[k] = value[:, i]
elif is_list_like(value):
self.obj[k] = value[i]
else:
self.obj[k] = value
keys = self.obj.columns.tolist()
Copy link
Contributor

Choose a reason for hiding this comment

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

can you create the keys in the same order whether they are in the obj or not (e.g. combine L666 and 667)

Copy link
Contributor

Choose a reason for hiding this comment

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

do we have a test for this ordering?

Copy link
Member Author

Choose a reason for hiding this comment

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

Union should do the trick.

At least the test added with #37964 covers this

keys.extend([k for k in key if k not in self.obj])
jreback marked this conversation as resolved.
Show resolved Hide resolved
self.obj._mgr = self.obj._mgr.reindex_axis(keys, 0)

def __setitem__(self, key, value):
if isinstance(key, tuple):
Expand Down