From 5a844974a393c59915ee01ef4d34cea6c0b6f6fd Mon Sep 17 00:00:00 2001 From: Simon Hawkins Date: Tue, 1 Dec 2020 08:52:59 +0000 Subject: [PATCH] Revert "ENH: Improve performance for df.__setitem__ with list-like indexers (#38148)" This reverts commit 2f4110967c303c739abef67be63ba2206d64bbb9. --- asv_bench/benchmarks/indexing.py | 8 -------- doc/source/whatsnew/v1.1.5.rst | 1 - pandas/core/indexing.py | 13 +++++++++++-- 3 files changed, 11 insertions(+), 11 deletions(-) diff --git a/asv_bench/benchmarks/indexing.py b/asv_bench/benchmarks/indexing.py index 4fd91c8aafe4b..74e0a3a434cde 100644 --- a/asv_bench/benchmarks/indexing.py +++ b/asv_bench/benchmarks/indexing.py @@ -358,14 +358,6 @@ 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: diff --git a/doc/source/whatsnew/v1.1.5.rst b/doc/source/whatsnew/v1.1.5.rst index 8cf35757861ed..4770ab37e08d2 100644 --- a/doc/source/whatsnew/v1.1.5.rst +++ b/doc/source/whatsnew/v1.1.5.rst @@ -24,7 +24,6 @@ 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`) - Fixed regression in :meth:`MultiIndex.intersection` returning duplicates when at least one of the indexes had duplicates (:issue:`36915`) .. --------------------------------------------------------------------------- diff --git a/pandas/core/indexing.py b/pandas/core/indexing.py index 28e59df995a83..f6cf691ea911c 100644 --- a/pandas/core/indexing.py +++ b/pandas/core/indexing.py @@ -672,8 +672,17 @@ 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) ): - keys = self.obj.columns.union(key, sort=False) - self.obj._mgr = self.obj._mgr.reindex_axis(keys, 0) + 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 def __setitem__(self, key, value): if isinstance(key, tuple):