Skip to content

Commit

Permalink
Backport PR pandas-dev#38148: ENH: Improve performance for df.__setit…
Browse files Browse the repository at this point in the history
…em__ with list-like indexers
  • Loading branch information
phofl authored and simonjayhawkins committed Nov 30, 2020
1 parent 45c1016 commit 335d100
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
8 changes: 8 additions & 0 deletions asv_bench/benchmarks/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,14 @@ 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):
np.random.seed(1234)
self.df[list(range(100))] = np.random.randn(self.N, 100)

def time_assign_list_of_columns_concat(self):
df = DataFrame(np.random.randn(self.N, 100))
concat([self.df, df], axis=1)


class ChainIndexing:

Expand Down
1 change: 1 addition & 0 deletions doc/source/whatsnew/v1.1.5.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Fixed regressions
- Fixed regression in :meth:`DataFrame.groupby` aggregation with out-of-bounds datetime objects in an object-dtype column (:issue:`36003`)
- Fixed regression in ``df.groupby(..).rolling(..)`` with the resulting :class:`MultiIndex` when grouping by a label that is in the index (:issue:`37641`)
- Fixed regression in :meth:`DataFrame.fillna` not filling ``NaN`` after other operations such as :meth:`DataFrame.pivot` (:issue:`36495`).
- Fixed performance regression for :meth:`DataFrame.__setitem__` with list-like indexers (:issue:`37954`)

.. ---------------------------------------------------------------------------
Expand Down
5 changes: 2 additions & 3 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -654,9 +654,8 @@ def _ensure_listlike_indexer(self, key, axis=None):
and not com.is_bool_indexer(key)
and all(is_hashable(k) for k in key)
):
for k in key:
if k not in self.obj:
self.obj[k] = np.nan
keys = self.obj.columns.union(key, sort=False)
self.obj._mgr = self.obj._mgr.reindex_axis(keys, 0)

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

0 comments on commit 335d100

Please sign in to comment.