Skip to content

Retain views with listlike indexers setitem #38204

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
Dec 2, 2020
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
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.2.0.rst
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ Performance improvements
- Performance improvement in :meth:`Series.astype` and :meth:`DataFrame.astype` for :class:`Categorical` (:issue:`8628`)
- Performance improvement in :meth:`DataFrame.groupby` for ``float`` ``dtype`` (:issue:`28303`), changes of the underlying hash-function can lead to changes in float based indexes sort ordering for ties (e.g. :meth:`Index.value_counts`)
- Performance improvement in :meth:`pd.isin` for inputs with more than 1e6 elements (:issue:`36611`)
- Performance improvement for :meth:`DataFrame.__setitem__` with list-like indexers (:issue:`37954`)

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

Expand Down
17 changes: 6 additions & 11 deletions pandas/core/indexing.py
Original file line number Diff line number Diff line change
Expand Up @@ -672,17 +672,12 @@ 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
# GH#38148
keys = self.obj.columns.union(key, sort=False)

self.obj._mgr = self.obj._mgr.reindex_axis(
keys, axis=0, copy=False, consolidate=False, only_slice=True
)

def __setitem__(self, key, value):
if isinstance(key, tuple):
Expand Down
17 changes: 15 additions & 2 deletions pandas/core/internals/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -1236,6 +1236,8 @@ def reindex_axis(
limit=None,
fill_value=None,
copy: bool = True,
consolidate: bool = True,
only_slice: bool = False,
):
"""
Conform block manager to new index.
Expand All @@ -1246,7 +1248,13 @@ def reindex_axis(
)

return self.reindex_indexer(
new_index, indexer, axis=axis, fill_value=fill_value, copy=copy
new_index,
indexer,
axis=axis,
fill_value=fill_value,
copy=copy,
consolidate=consolidate,
only_slice=only_slice,
)

def reindex_indexer(
Expand All @@ -1258,6 +1266,7 @@ def reindex_indexer(
allow_dups: bool = False,
copy: bool = True,
consolidate: bool = True,
only_slice: bool = False,
) -> T:
"""
Parameters
Expand All @@ -1270,6 +1279,8 @@ def reindex_indexer(
copy : bool, default True
consolidate: bool, default True
Whether to consolidate inplace before reindexing.
only_slice : bool, default False
Whether to take views, not copies, along columns.

pandas-indexer with -1's only.
"""
Expand All @@ -1293,7 +1304,9 @@ def reindex_indexer(
raise IndexError("Requested axis not found in manager")

if axis == 0:
new_blocks = self._slice_take_blocks_ax0(indexer, fill_value=fill_value)
new_blocks = self._slice_take_blocks_ax0(
indexer, fill_value=fill_value, only_slice=only_slice
)
else:
new_blocks = [
blk.take_nd(
Expand Down
18 changes: 18 additions & 0 deletions pandas/tests/frame/indexing/test_setitem.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,24 @@ def test_setitem_bool_with_numeric_index(self, dtype):
tm.assert_index_equal(df.columns, expected_cols)


class TestDataFrameSetItemWithExpansion:
def test_setitem_listlike_views(self):
# GH#38148
df = DataFrame({"a": [1, 2, 3], "b": [4, 4, 6]})

# get one column as a view of df
ser = df["a"]

# add columns with list-like indexer
df[["c", "d"]] = np.array([[0.1, 0.2], [0.3, 0.4], [0.4, 0.5]])

# edit in place the first column to check view semantics
df.iloc[0, 0] = 100

expected = Series([100, 2, 3], name="a")
tm.assert_series_equal(ser, expected)


class TestDataFrameSetItemSlicing:
def test_setitem_slice_position(self):
# GH#31469
Expand Down